client.event 不向 json 文件添加前缀 on_guild_add (discord.py)
client.event does not add prefix to json file on_guild_add (discord.py)
我正在尝试在 json 中添加 bot 时创建事件 bot 添加前缀:
@client.event
async def on_guild_join(guild):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(guild.id)] = '//'
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
但我不明白为什么会出现这个错误:
Traceback (most recent call last):
File "c:/projects/bots/textjson.py", line 14, in <module>
@client.event
NameError: name 'client' is not defined
我的代码有问题吗???
还有导入脚本
import discord
import json
from discord.ext import commands
错误提示找不到名为 client
的变量。你给下面的名字起了什么名字?
... = commands.Bot(command_prefix=...
通常人们将其命名为 client
或 bot
,因此请仔细检查您引用的是您认为的名称。
出现这种情况的另一个原因是,如果您将事件 放在定义 client
变量的行 上方:
@client.event
async def on_guild_join(.....
client = commands.Bot(.....
应该像这样重新排序:
client = commands.Bot(.....
@client.event
async def on_guild_join(.....
我正在尝试在 json 中添加 bot 时创建事件 bot 添加前缀:
@client.event
async def on_guild_join(guild):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(guild.id)] = '//'
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
但我不明白为什么会出现这个错误:
Traceback (most recent call last):
File "c:/projects/bots/textjson.py", line 14, in <module>
@client.event
NameError: name 'client' is not defined
我的代码有问题吗???
还有导入脚本
import discord
import json
from discord.ext import commands
错误提示找不到名为 client
的变量。你给下面的名字起了什么名字?
... = commands.Bot(command_prefix=...
通常人们将其命名为 client
或 bot
,因此请仔细检查您引用的是您认为的名称。
出现这种情况的另一个原因是,如果您将事件 放在定义 client
变量的行 上方:
@client.event
async def on_guild_join(.....
client = commands.Bot(.....
应该像这样重新排序:
client = commands.Bot(.....
@client.event
async def on_guild_join(.....