所以我已经使用discord.py有一段时间了,最近切换到discord.py-rewrite。我似乎找不到任何关于如何做到这一点的信息。但我想要每个公会的数据。例如,如果机器人位于 Server 1
中,并且 Owner1
执行 -prefix !!
,它会将其添加到其前缀中,但仅限于其公会中。因此它不会是 Server2
中的前缀。任何帮助都会很棒。有人告诉我可以使用 JSON
文件执行此操作,但在其中找不到任何我想要的内容。
最佳答案
我自己就是一个不和谐的机器人制造者,有时我会遇到这个问题。 我为此创建了一个解决方法。
你需要的是字典而不是变量。 字典的键是公会属性,值是该公会的日期。
这是我的代码片段:
# Changing variables and setting them
#--------------------------------------------------------------
# Changing the values of a dictionary.
def guildvarchg(variable, guild, value): # Guild Variable Change
for key in variable.keys(): # Do not change any of this.
if key == guild:
variable[key] = value
# To get a certain guild's variable, add [guild id] to the dictionary name.
# For example: variable[ctx.author.guild] # takes the data for that guild
# You are going to run into a first time setup issue.
# To counter this, and for every new guild, add this if statement:
if ctx.author.guild not in variable: # only use this in a command definition
variable[ctx.author.guild] = 'New value' # adds a new server to the data.
else:
guildvarchg(variable, ctx.author.guild, new_value) # <<< overwrites the server data
#--------------------------------------------------------------
建议您在运行之前创建所有变量[字典]
variable{None:None} # Format after setting: variable{guild_id, data}
关于python - Discord.py重写多服务器数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52667764/