简体   繁体   English

如何使用命令discord.py忽略用户ID

[英]How to make ignore a userid using the command discord.py

This is the code: 这是代码:

@bot.command(name="add")
async def _blacklist_add(self, user: discord.Member):
        """Adds user to bot's blacklist"""
        if user.id not in self.blacklist_list:
            self.blacklist_list.append(user.id)
            fileIO("blacklist.json", "save", self.blacklist_list)
            await self.bot.say("User has been added to blacklist.")
        else:
            await self.bot.say("User is already blacklisted.")

@bot.command(name="remove")
async def _blacklist_remove(self, user: discord.Member):
        """Removes user to bot's blacklist"""
        if user.id in self.blacklist_list:
            self.blacklist_list.remove(user.id)
            fileIO("blacklist.json", "save", self.blacklist_list)
            await self.bot.say("User has been removed from blacklist.")
        else:
            await self.bot.say("User is not in blacklist.")

I want to know how to import JSON so that I can stop use abusing my bot, if you can help me. 我想知道如何导入JSON,以便在可以帮助我的情况下停止使用滥用我的机器人的方法。

You can use the json module. 您可以使用json模块。

Assuming you have a file in the same directory as your bot (named "ids.json"), which you want to load the IDs from: 假设您有一个文件与您的漫游器位于同一目录(名为“ ids.json”),您希望从以下文件中加载ID:

import json

with open("ids.json", "r") as f:
    ids = json.load(f)

All you need to put in the "ids.json" file is 您需要放入“ ids.json”文件中的所有内容是

["249928002161344512", "387539916525142016", "359951141343068182", "308293489827774465"]

You are now free to use the ids variable in the same way as you are now. 现在,您可以像现在一样自由使用ids变量。

If you want to save the content of the ids variable to the file, you can use json.dump(ids, f) 如果要将ids变量的内容保存到文件中,可以使用json.dump(ids, f)

with open("ids.json", "w") as f:
    json.dump(ids, f)

However, you do need to be careful of using the file too much. 但是,您确实需要过多使用该文件。 Dumping a lot can cause issues where data is not saved to the file properly. 大量转储会导致数据无法正确保存到文件的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM