问题
trying to create a command for my bot where you can type /ping @user, and it then dm's mentioned user, as well as replies in chat with 'pong'
heres what i have so far:
async def cmd_ping(self, author, user_mentions):
if not user_mentions:
raise exceptions.CommandError("No users listed.", expire_in=20)
else:
usr = user_mentions[0]
await self.safe_send_message(mention_user, "`pong!`")
return Response(self.str.get('mentioned-user', "***{0}*** `pong`").format(usr.name,))
without await self.safe_send_message(mention_user, "pong!
") the command works fine and replied with pong in chat
edit ~ i did not write this, i'm going off from another bot as i am not well knowledged in this field
async def safe_send_message(self, dest, content, **kwargs):
tts = kwargs.pop('tts', False)
quiet = kwargs.pop('quiet', False)
expire_in = kwargs.pop('expire_in', 0)
allow_none = kwargs.pop('allow_none', True)
also_delete = kwargs.pop('also_delete', None)
msg = None
lfunc = log.debug if quiet else log.warning
try:
if content is not None or allow_none:
if isinstance(content, discord.Embed):
msg = await self.send_message(dest, embed=content)
else:
msg = await self.send_message(dest, content, tts=tts)
except discord.Forbidden:
lfunc("Cannot send message to \"%s\", no permission", dest.name)
except discord.NotFound:
lfunc("Cannot send message to \"%s\", invalid channel?", dest.name)
except discord.HTTPException:
if len(content) > DISCORD_MSG_CHAR_LIMIT:
lfunc("Message is over the message size limit (%s)", DISCORD_MSG_CHAR_LIMIT)
else:
lfunc("Failed to send message")
log.noise("Got HTTPException trying to send message to %s: %s", dest, content)
finally:
if msg and expire_in:
asyncio.ensure_future(self._wait_delete_msg(msg, expire_in))
if also_delete and isinstance(also_delete, discord.Message):
asyncio.ensure_future(self._wait_delete_msg(also_delete, expire_in))
return msg
回答1:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='/')
@bot.command()
async def ping(user: discord.User):
await bot.send_message(user, "Pong")
await bot.say("Pong")
@ping.error
async def ping_error(ctx, error):
# You might have to play with the arguments here depending on your version of discord.py
if isinstance(error, commands.BadArgument):
await bot.say("Could not find that user.")
I've tried to write the above for "async" branch, but you should know that there is also a "rewrite" branch of discord.py
that is more current and has better documentation, especially when it comes to the commands
extension.
Did you write safe_send_message
? I don't see it documented anywhere.
回答2:
This is my warn command. Try to use this as a template.
@client.command(pass_context = True)
@commands.has_permissions(kick_members=True)
async def warn(ctx, userName: discord.User, *, message:str):
await client.send_message(userName, "You have been warned for: {}".format(message))
await client.say(":warning: __**{0} Has Been Warned!**__ :warning: Reason:{1} ".format(userName,message))
pass
来源:https://stackoverflow.com/questions/50076814/having-trouble-making-a-discord-bot-that-direct-messages-mentioned-user