首页 > 解决方案 > Discord.py 机器人禁止一个人

问题描述

有没有办法我们可以禁止一个人使用机器人的命令。基本上给那个特定的人一个机器人禁令!

不和谐.py

标签: discord.pydiscord.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")

推荐阅读