discord.py - Discord.py 机器人禁止一个人
问题描述
有没有办法我们可以禁止一个人使用机器人的命令。基本上给那个特定的人一个机器人禁令!
不和谐.py
解决方案
您只需以您喜欢的任何方式(列表、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")
推荐阅读
- c++ - Visual Studio 中的警告 C6385
- c++ - cmake 使用 find_package 传播依赖项
- sql - 寻找提供独特产品的供应商
- heroku - Heroku 构建日志和日志显示没有错误但应用程序无法运行
- javascript - 使用路由器请求时如何在JS中捕获错误
- javascript - 检测对象的某个属性的值为空
- spring-boot - Spring Boot 2.x Repackage 用于使用 gradle 进行集成测试
- oracle - Oracle APEX:如何使用:m 关系连接两个表
- git - 如何从 git config 中的跟踪分支重新初始化头?
- android - 改造不通过 https 发送标头