繁体   English   中英

名称“公会”未定义| discord.py

[英]name 'guild' is not defined | discord.py

我正在编写一个机器人来在我的 discord 服务器上查找具有“管理员”角色的用户,并对他们做一些事情。 到目前为止,我已经写了这个:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="$")
role_name = "Admin"
peopleWithRole = []

@bot.event
async def on_ready():
    print("Logged in as")
    print(bot.user.name)
    print("------")
    role = discord.utils.find(
        lambda r: r.name == role_name, guild.roles)
        
    for user in guild.members:
        if role in user.roles:
            peopleWithRole.append(user)

bot.run("My token")

但是,当我运行它时,我收到一条错误消息,指出未定义名称“公会”。 我刚从 discord.py 开始,我也想知道在这种情况下我应该使用客户端还是机器人。

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix="$")
role_name = "Admin"
peopleWithRole = []

@bot.event
async def on_ready():
    print("Logged in as")
    print(bot.user.name)
    print("------")
    guild = bot.guilds[0]

    role = discord.utils.find(
        lambda r: r.name == role_name, guild.roles)
        
    for user in guild.members:
        if role in user.roles:
            peopleWithRole.append(user)

bot.run("My token")

在您的 find 调用中,您引用了 guild.roles 但从未定义过公会。 你需要 select 公会(我相信公会是机器人的成员)

这是一个相当基本的 python 错误要调试,任何 IDE 都会识别出什么是错误的。 我建议您查看一些有关如何调试的教程。

暂无
暂无

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

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