python - Discord.py重写多服务器数据

标签 python python-3.x discord discord.py

所以我已经使用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/

相关文章:

python - 从 Django 应用程序使用 Ruby gem

python - 在 Python 中以一定角度(旋转)绘制文本

python - 对类属性进行假继承是否不好

javascript - DM'ing 一个特定的用户 ID

python-3.x - Python Discord Bot 禁止列表中的人员

python - fabric 脚本中的 Fdisk 分区

python - 从不运行 Django 的站点使用 mod_wsgi 服务 Django 应用程序/

不使用表单而只使用 Flask 作为 API 上传文件的 Python 脚本

python - python 3中字符串切片操作的时间复杂度

SonarQube 不会向 Discord webhook 发送通知