【发布时间】:2023-03-15 00:21:01
【问题描述】:
有没有一种方法可以禁止某人使用机器人的命令。基本上是给那个特定的人一个机器人禁令!
Discord.py
【问题讨论】:
标签: discord.py discord.py-rewrite
有没有一种方法可以禁止某人使用机器人的命令。基本上是给那个特定的人一个机器人禁令!
Discord.py
【问题讨论】:
标签: discord.py discord.py-rewrite
做一个禁止命令,然后当运行禁止命令而不是禁止你给他们一个被禁止角色的人时,所以每当一个命令运行时,你可以检查某人是否具有该角色,如果他们执行该命令将不起作用
【讨论】:
您可以简单地以您喜欢的任何方式(列表、json、txt 文件或任何数据库)存储被禁止用户的 id,然后当用户使用命令时,机器人将检查用户的 id 是否已存储。
您也可以执行命令将用户 ID 添加到列表中,但请记住,新 ID 不会永久存储,换句话说,如果您关闭机器人,数据将会消失。
简单示例:
#stored ids
BANNED_USERS = [1234567890, 0987654321]
@client.command()
async def check(ctx):
#check if the user is banned
if ctx.author.id in BANNED_USERS:
await ctx.send("you are banned from using this command")
#if the user is not banned
else:
await ctx.send("you are allowed to use this command")
@client.command()
async def blacklist(ctx, member: discord.Member):
BANNED_USERS.append(member.id)
await ctx.send(f"{member} has been added to the blacklist")
如果你想使用.txt文件的方式,这里有一个简单的例子:
By this way the user id will be stored in the text file that means if you turned off your bot, the banned users They'll keep stored unlike the list one
@client.command()
async def check(ctx):
file = open("banned.txt", "r")
members_banned = file.readlines()
if str(ctx.author.id) in members_banned:
await ctx.send("you are not allowed to use my commands")
else:
await ctx.send("you are allowed to use my commands")
file.close()
@client.command()
async def blacklist(ctx, member: discord.Member):
file = open("banned.txt", "a")
file.write(member.id)
file.close()
await ctx.send(f"{member} has been added to the blacklist")
【讨论】: