制作一个指导消息@ disclied用户的discord机器人时遇到问题

时间:2018-04-28 13:01:10

标签: python python-3.x discord.py discord.py-rewrite

尝试为我的机器人创建一个命令,你可以输入/ 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,))

没有等待self.safe_send_message(提及用户," 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

2 个答案:

答案 0 :(得分: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.")

我试过写上面的#34; async"分支,但你应该知道还有一个"重写" discord.py的分支,它更新,并且有更好的文档,特别是涉及commands扩展时。

你写过safe_send_message吗?我没有在任何地方看到它。

答案 1 :(得分:1)

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

@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