本文介绍了制作一个不和谐的机器人来引导@提到的用户的消息时遇到麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试为我的机器人创建一个命令,您可以在其中键入/ ping @user,然后输入dm提到的用户以及与 pong聊天时的回复

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,))

无需等待self.safe_send_message(mentionuser, pong!)命令运行正常,并在聊天中回复pong

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


推荐答案

这是我的警告命令。尝试将其用作模板。

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

这篇关于制作一个不和谐的机器人来引导@提到的用户的消息时遇到麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 16:15