I have recently started programming my own discord bot as many other people are doing... So far what I have got is this:
@bot.command
async def on_message(message):
if message.content.startswith("t!send"):
await client.send_message(message.content)
It doesn't crash but it doesn't work either...
It seems as if you're using a tutorial for an old version of discord.py. There are some big changes in the most recent version - rewrite. Please take some time to look for more updated tutorials, or read the documentation linked above.
Here are the cases for using both the on_message
event and commands:
@bot.command()
async def send(ctx, *, sentence):
await ctx.send(sentence)
######################################
@bot.event
async def on_message(message):
args = message.content.split(" ")[1:]
if message.content.startswith("t!send"):
await message.channel.send(" ".join(args))
else:
await bot.process_commands(message) # only add this if you're also using command decorators
References:
commands.Context()
- if not familiar with using command decorators discord.on_message()
Bot.process_commands()
So first of all, on_message doesn't belong here and you also don't have to use it. ( on_message()
would use the decorator @bot.event
) Assuming you have setup a prefix for your bot using bot = commands.Bot(command_prefix = 't!')
you could do something like this:
@bot.command()
async def send(ctx, *args):
message = " ".join(args)
await ctx.send(message)
*args is everything the user types in after t.send: So for example: if the user types in t!send Hello world! args would be ["Hello", "world!"]
. With .join
we can join them together into a single string ( message
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.