DM命令破坏机器人 [英] DM commands to discord bot

查看:59
本文介绍了DM命令破坏机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近想到了DM机器人命令的想法.例如,该命令将禁止我进入该漫游器所在的每台服务器.

不幸的是,我没有该命令的任何起点,因为我什至不确定DMing命令是否可行.

discord.py command DM 之类的关键字在Google中非常常见,以至于很难找到关于该主题的任何好信息.

我正在寻找一种让bot接收DM作为命令并仅接受我的命令的方式(如果有人要共享任何代码,我的ID将存储在变量 ownerID 中)./p>

虽然我主要是在寻找上面的内容,但是DM unban 命令的一些代码也将非常有帮助.

我被要求显示我的机器人的一些示例代码.这是命令 number 的代码,该命令生成一个随机数并将其发送给消息.我希望这能使您了解我的机器人的制造方式:

  @ BSL.command(pass_context = True)异步定义号(ctx,minInt,maxInt):如果是ctx.message.author.server_permissions.send_messages或ctx.message.author.id == ownerID:maxInt =整数(maxInt)minInt =整数(minInt)randomInt = random.randint(minInt,maxInt)randomInt = str(randomInt)等待BSL.send_message(ctx.message.channel,'您的随机数为:'+ randomInt)别的:等待BSL.send_message(ctx.message.channel,'抱歉,您无权执行@@}!'.format(ctx.message.author)) 

解决方案

您可以通过私人消息发送命令.像

  @ BSL.command(pass_context = True)异步def unban(ctx):如果ctx.message.channel.is_private和ctx.message.author.id == ownerID:所有者=等待BSL.get_user_info(ownerID)等待BSL.say('确定{},取消您的权限.'.format(owner.name))对于BSL.servers中的服务器:尝试:等待BSL.unban(服务器,所有者)#我认为使用相同的名称应该可以.如果您看到奇怪的错误,这可能就是原因.等待BSL.say('从{}'开始禁止.format(server.name))除了discord.HTTPException作为e:等待BSL.say(在{}中取消限制失败,因为\ n {}".format(server.name,e.text))除不和谐外,禁止:等待BSL.say('禁止以{}'.format(server.name)取消禁止)别的:如果ctx.message.author.id!= ownerID:等待BSL.say('您不是我的所有者')如果不是ctx.message.channel.is_private:等待BSL.say('这是一个公共频道') 

应该工作.我不确定如果尝试取消禁止未被禁止的用户会发生什么,这可能会引发 HTTPException

I recently had the idea of DMing my bot commands. For example, a command which would unban me from every server that the bot is on.

Unfortunately I don't have any starting point for the command because I'm not even sure DMing commands is possible.

Keywords like discord.py, command or DM are so common in google that finding any good info on the topic is very hard.

I'm looking for a way for the bot to receive DMs as commands and only accept them from me (my ID is stored in the variable ownerID if anyone wants to share any code).

While I'm mostly looking for the above, some code for the DM unban command would also be very helpful.

EDIT: I was asked to show some example code from my bot. Here is the code for the command number which generates a random number and sends it in a message. I hope this gives you an idea of how my bot is made:

@BSL.command(pass_context = True)
async def number(ctx, minInt, maxInt):
    if ctx.message.author.server_permissions.send_messages or ctx.message.author.id == ownerID:
        maxInt = int(maxInt)
        minInt = int(minInt)
        randomInt = random.randint(minInt, maxInt)
        randomInt = str(randomInt)
        await BSL.send_message(ctx.message.channel, 'Your random number is: ' + randomInt)
    else:
        await BSL.send_message(ctx.message.channel, 'Sorry, you do not have the permissions to do that @{}!'.format(ctx.message.author))

解决方案

You can send commands in private messages. Something like

@BSL.command(pass_context=True)
async def unban(ctx):
    if ctx.message.channel.is_private and ctx.message.author.id == ownerID:
        owner = await BSL.get_user_info(ownerID)
        await BSL.say('Ok {}, unbanning you.'.format(owner.name))
        for server in BSL.servers:
            try:
                await BSL.unban(server, owner) # I think having the same name should be fine.  If you see weird errors this may be why.
                await BSL.say('Unbanned from {}'.format(server.name))
            except discord.HTTPException as e:
                await BSL.say('Unbanning failed in {} because\n{}'.format(server.name, e.text))
            except discord.Forbidden:
                await BSL.say('Forbidden to unban in {}'.format(server.name))
    else:
        if ctx.message.author.id != ownerID:
            await BSL.say('You are not my owner')
        if not ctx.message.channel.is_private:
            await BSL.say('This is a public channel')

should work. I'm not sure what happens if you try to unban a user who is not banned, that may be what raises HTTPException

这篇关于DM命令破坏机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆