So, I have a Python program using discord.py where I am trying to have functions manage different message commands, but when I put the "await" in the function, it gives me a syntax error because it's not in the async. Any way I can get around this?
def selectedCommand(message):
await client.send_message(stuff in here)
#client.event
async def on_message(message):
selectedCommand(message)
#client.event
async def on_edit_message(message):
selectedCommand(message)
You need to make selectedCommand a coroutine (an async def function) as well, and then await it when it's called:
async def selectedCommand(message):
await client.send_message(stuff in here)
#client.event
async def on_message(message):
await selectedCommand(message)
Related
Basically, everything appears to work fine and start up, but for some reason I can't call any of the commands. I've been looking around for easily an hour now and looking at examples/watching videos and I can't for the life of me figure out what is wrong. Code below:
import discord
import asyncio
from discord.ext import commands
bot = commands.Bot(command_prefix = '-')
#bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
#bot.event
async def on_message(message):
if message.content.startswith('-debug'):
await message.channel.send('d')
#bot.command(pass_context=True)
async def ping(ctx):
await ctx.channel.send('Pong!')
#bot.command(pass_context=True)
async def add(ctx, *, arg):
await ctx.send(arg)
The debug output I have in on_message actually does work and responds, and the whole bot runs wihout any exceptions, but it just won't call the commands.
From the documentation:
Overriding the default provided on_message forbids any extra commands from running. To fix this, add a bot.process_commands(message) line at the end of your on_message. For example:
#bot.event
async def on_message(message):
# do some extra stuff here
await bot.process_commands(message)
The default on_message contains a call to this coroutine, but when you override it with your own on_message, you need to call it yourself.
Ascertained that the problem stems from your definition of on_message, you could actually just implement debug as a command, since it appears to have the same prefix as your bot:
#bot.command()
async def debug(ctx):
await ctx.send("d")
I am trying to write a bot for Discord but I keep getting an error message:
File "bot.py", line 33
await member.create_dm()
^
SyntaxError: 'await' outside async function
I am trying to get the bot to send a DM to a person who has just joined the server.
#client.event
#asyncio.coroutine
def on_member_join(member):
await member.create_dm()
member.dm_channel.send("Hi and welcome!")
I would very much appreciate your help.
Your code should look like this:
#client.event
#asyncio.coroutine
async def on_member_join(member):
await member.create_dm()
member.dm_channel.send("Hi and welcome!")
add async before def
for more information about async in python consider reading this: https://docs.python.org/3/library/asyncio-task.html
Basically, everything appears to work fine and start up, but for some reason I can't call any of the commands. I've been looking around for easily an hour now and looking at examples/watching videos and I can't for the life of me figure out what is wrong. Code below:
import discord
import asyncio
from discord.ext import commands
bot = commands.Bot(command_prefix = '-')
#bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
#bot.event
async def on_message(message):
if message.content.startswith('-debug'):
await message.channel.send('d')
#bot.command(pass_context=True)
async def ping(ctx):
await ctx.channel.send('Pong!')
#bot.command(pass_context=True)
async def add(ctx, *, arg):
await ctx.send(arg)
The debug output I have in on_message actually does work and responds, and the whole bot runs wihout any exceptions, but it just won't call the commands.
From the documentation:
Overriding the default provided on_message forbids any extra commands from running. To fix this, add a bot.process_commands(message) line at the end of your on_message. For example:
#bot.event
async def on_message(message):
# do some extra stuff here
await bot.process_commands(message)
The default on_message contains a call to this coroutine, but when you override it with your own on_message, you need to call it yourself.
Ascertained that the problem stems from your definition of on_message, you could actually just implement debug as a command, since it appears to have the same prefix as your bot:
#bot.command()
async def debug(ctx):
await ctx.send("d")
I have a running discord bot using Python, however, I am doing a rewrite to condense the code for other contributors to contribute code by adding an additional function that should reduce the reuse of code.
However, in the code, the function textCom is not able to recognize client.send_message
I have tried adding async to the def of the textCom function, however, this results in no response by the bot.
import discord
TOKEN = 'token'
client = discord.Client()
#client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
# commands cannot be executed in private messages
if message.channel.type == discord.ChannelType.private:
return
# original code
if message.content.startswith('!text1'):
msg = "text1 executed".format(message)
await client.send_message(message.channel, msg)
# new code I want executed
def textCom(textMessage):
msg = textMessage.format(message)
await client.send_message(message.channel, msg)
if message.content.startswith('!text2'):
textCom("text2 executed")
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.run(TOKEN)
The python script run, however, when a user types !text1 the bot replies with "text1 executed" but when a user types !text2 the bot does not reply.
The interpreter should be raising a syntax error when you define on_message, before the bot even starts. You can't have an await inside a non-async function.
Make textCom an async def function and await it when you call it.
#client.event
async def on_message(message):
...
async def textCom(textMessage):
msg = textMessage.format(message)
await client.send_message(message.channel, msg)
if message.content.startswith('!text2'):
await textCom("text2 executed")
Basically, everything appears to work fine and start up, but for some reason I can't call any of the commands. I've been looking around for easily an hour now and looking at examples/watching videos and I can't for the life of me figure out what is wrong. Code below:
import discord
import asyncio
from discord.ext import commands
bot = commands.Bot(command_prefix = '-')
#bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
#bot.event
async def on_message(message):
if message.content.startswith('-debug'):
await message.channel.send('d')
#bot.command(pass_context=True)
async def ping(ctx):
await ctx.channel.send('Pong!')
#bot.command(pass_context=True)
async def add(ctx, *, arg):
await ctx.send(arg)
The debug output I have in on_message actually does work and responds, and the whole bot runs wihout any exceptions, but it just won't call the commands.
From the documentation:
Overriding the default provided on_message forbids any extra commands from running. To fix this, add a bot.process_commands(message) line at the end of your on_message. For example:
#bot.event
async def on_message(message):
# do some extra stuff here
await bot.process_commands(message)
The default on_message contains a call to this coroutine, but when you override it with your own on_message, you need to call it yourself.
Ascertained that the problem stems from your definition of on_message, you could actually just implement debug as a command, since it appears to have the same prefix as your bot:
#bot.command()
async def debug(ctx):
await ctx.send("d")