0

尝试为我的机器人创建一个命令,您可以在其中键入 /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
4

2 回答 2

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
于 2018-05-07T11:32:22.893 回答
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.")

我已经尝试为“异步”分支编写上述内容,但您应该知道还有一个“重写”分支更新,discord.py并且具有更好的文档,尤其是在涉及commands扩展时。

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

于 2018-04-28T13:24:11.797 回答