簡體   English   中英

client.event 不向 json 文件 on_guild_add (discord.py) 添加前綴

[英]client.event does not add prefix to json file on_guild_add (discord.py)

當機器人被添加到 json 機器人添加前綴時,我試圖創建一個事件:

@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=...

通常人們會將此clientbot命名,因此請仔細檢查您是否引用了您認為的名稱。


這可能出現的另一個原因是,如果您將事件放在定義client變量的行之上

@client.event
async def on_guild_join(.....

client = commands.Bot(.....

應該像這樣重新排序:

client = commands.Bot(.....

@client.event
async def on_guild_join(.....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM