用于不和谐机器人的 DM 命令
DM commands to discord bot
我最近有了用 DM 发送我的机器人命令的想法。例如,一条命令可以将我从机器人所在的每台服务器上解禁。
不幸的是,我没有任何命令的起点,因为我什至不确定 DMing 命令是否可行。
discord.py
、command
或 DM
等关键字在 google 中非常常见,因此很难找到有关该主题的任何有用信息。
我正在寻找一种方法让机器人接收 DM 作为命令,并且只接受来自我的消息(如果有人想共享任何代码,我的 ID 存储在变量 ownerID
中)。
虽然我主要是在寻找上述内容,但 DM unban
命令的一些代码也会很有帮助。
编辑:我被要求展示我的机器人的一些示例代码。下面是命令 number
的代码,它生成一个随机数并在消息中发送它。我希望这能让您了解我的机器人是如何制作的:
@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))
您可以在私信中发送指令。像
@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')
应该可以。我不确定如果您尝试取消禁止未被禁止的用户会发生什么,这可能会引发 HTTPException
我最近有了用 DM 发送我的机器人命令的想法。例如,一条命令可以将我从机器人所在的每台服务器上解禁。
不幸的是,我没有任何命令的起点,因为我什至不确定 DMing 命令是否可行。
discord.py
、command
或 DM
等关键字在 google 中非常常见,因此很难找到有关该主题的任何有用信息。
我正在寻找一种方法让机器人接收 DM 作为命令,并且只接受来自我的消息(如果有人想共享任何代码,我的 ID 存储在变量 ownerID
中)。
虽然我主要是在寻找上述内容,但 DM unban
命令的一些代码也会很有帮助。
编辑:我被要求展示我的机器人的一些示例代码。下面是命令 number
的代码,它生成一个随机数并在消息中发送它。我希望这能让您了解我的机器人是如何制作的:
@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))
您可以在私信中发送指令。像
@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')
应该可以。我不确定如果您尝试取消禁止未被禁止的用户会发生什么,这可能会引发 HTTPException