尝试为我的机器人创建一个命令,您可以在其中键入 /ping @user,然后它会 dm 提到的用户,以及与“pong”聊天的回复
继承人我到目前为止:
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,))
没有 await self.safe_send_message(mention_user, " pong!
") 该命令可以正常工作并在聊天中回复 pong
编辑〜我没有写这个,我要离开另一个机器人,因为我对这个领域不太了解
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