英文:
AttributeError: 'NoneType' object has no attribute 'get_role'
问题
我试图获取具有特定角色的所有用户,但我不知道如何获取这个特定角色,我搜索并找到了`guild.get_role(ROLE_ID)`,但不起作用;-;
import discord
from discord.ext import commands
import os
TOKEN = # token
GUILD_ID = # guild id
ROLE_ID = # role id
bot = commands.Bot(command_prefix='+', intents=discord.Intents.all())
guild = bot.get_guild(GUILD_ID)
admin_role = guild.get_role(ROLE_ID)
bot.run(TOKEN)
错误:
`AttributeError: 'NoneType' object has no attribute 'get_role'`
我不知道如何解决,求帮助;-;
英文:
I'm trying to get all users with a specific role but idk how to get this specific role, I searched and found guild.get_role(ROLE_ID)
, but doesn't work ;-;
import discord
from discord.ext import commands
import os
TOKEN = # token
GUILD_ID = # guild id
ROLE_ID = # role id
bot = commands.Bot(command_prefix='+', intents=discord.Intents.all())
guild = bot.get_guild(GUILD_ID)
admin_role = guild.get_role(ROLE_ID)
bot.run(TOKEN)
error:
AttributeError: 'NoneType' object has no attribute 'get_role'
I don't know how to resolve HELP ;-;
答案1
得分: 1
你的变量 guild
是 None
,因为你必须等待你的机器人成功连接到Discord的服务器。为此,你可以简单地监听 on_ready
机器人事件。
import discord
from discord.ext import commands
import os
TOKEN = # token
GUILD_ID = # guild id
ROLE_ID = # role id
bot = commands.Bot(command_prefix='+', intents=discord.Intents.all())
@bot.event
async def on_ready():
guild = bot.get_guild(GUILD_ID)
admin_role = guild.get_role(ROLE_ID)
# [...]
bot.run(TOKEN)
还请确保你的公会ID是正确的且是整数。否则 bot.get_guild()
仍然会返回 None
。
英文:
Your variable guild
is None
because you have to wait for your bot to successfully connect to Discord's servers first. For this, you can simply listen for the on_ready
bot event.
import discord
from discord.ext import commands
import os
TOKEN = # token
GUILD_ID = # guild id
ROLE_ID = # role id
bot = commands.Bot(command_prefix='+', intents=discord.Intents.all())
@bot.event
async def on_ready():
guild = bot.get_guild(GUILD_ID)
admin_role = guild.get_role(ROLE_ID)
# [...]
bot.run(TOKEN)
Also make sure that your guild ID is correct and an Integer. Otherwise bot.get_guild()
will still return None
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论