Broadcast command in discord.py

ぃ、小莉子 提交于 2019-12-04 06:54:29

问题


I am wanting to implement a cool broadcast command. The command will be like this. If an admin DMs the bot with $bc <whatever they want to type here> then the bot will then send that message to #broadcast. It can read from DMs and the server. I tried this command but it didn't work as I would have thought.

@bot.command(pass_context=True)
async def bc(args, message):
    channel = server.get_channel("474316889435275264")
    await bot.send_message(channel, args)

It can't read any of the channel = server.get_channel("474316889435275264") so I know that the code is wrong.

PS: it would be like the "say" command but it would send it to a specified channel.


回答1:


You can use Client.get_channel to resolve the channel even if you don't have a reference to the server. I assume below that you always want to send the message to a specific channel no matter where it comes from.

discord.py also has special handling of keyword only arguments that allows them to capture the remaining unprocessed text of a message, instead of being split into arguments. In your example, bc(args, message), message would only capture a single word or force you to wrap the message in quotes.

@bot.command(pass_context=True)
async def bc(ctx, *, message):
    channel = bot.get_channel("474316889435275264")
    await bot.send_message(channel, message)


来源:https://stackoverflow.com/questions/51959760/broadcast-command-in-discord-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!