问题
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