Discord.py AttributeError: "User" object has no attribute "Guild"
Discord.py AttributeError: "User" object has no attribute "Guild"
我正在开发自定义 discord 机器人,主要是为了好玩。我从另一台服务器上找到了另一个名为 Kurisu 的 discord 机器人,它是完全自定义和开源的。这是 github: https://github.com/nh-server/Kurisu. In mod.py (https://github.com/nh-server/Kurisu/blob/port/cogs/mod.py),在第 432 行,有一个功能基本上为用户提供了 No-Help 角色(在服务器中这限制了对特定频道的访问)。我正在尝试做类似的事情,其中具有无语音角色会限制您对语音频道的访问。我正在使用他们的一些代码,即在函数的参数中。我正在执行 async def takevoice(ctx, member: FetchMember): 作为函数。完整功能在这里:
@bot.command(name="takevoice", pass_context=True, description="Removes access to voice channels. Admin only.")
@commands.has_role("Admin") # This must be exactly the name of the appropriate role
async def takevoice(ctx, member: FetchMember):
role = get(member.guild.roles, name="No-Voice")
await member.add_roles(role)
我程序中的其他函数运行良好,但每当我尝试 运行 此函数以另一个用户为目标时,它都不起作用并给我一个回溯错误。它的结尾是:AttributeError:“用户”对象没有属性“公会”。我到处寻找答案,但找不到。任何帮助将不胜感激。
谢谢!
我的机器人的完整代码在这里:(我正在使用其他几个文件,所有这些都可以在 Kurisu github 的 utils 文件夹中找到。也就是说,我正在使用 checks.py、converters.py、database.py、manager.py 和 utils.py。我还使用了一个名为 keep_alive.py 的文件,因为我正在 运行 宁这个在 repl.it.)
import asyncio
import aiohttp
import json
import keep_alive
from discord import Game
from discord.ext.commands import Bot
import datetime
import re
import time
from subprocess import call
import discord
from discord.ext import commands
from checks import is_staff, check_staff_id, check_bot_or_staff
from database import DatabaseCog
from converters import SafeMember, FetchMember
import utils
from discord.ext import commands
from discord.utils import get
TOKEN = "" # Get at discordapp.com/developers/applications/me
bot=commands.Bot(command_prefix=".")
client=discord.Client()
@bot.command(name="hi")
async def hi(ctx):
await ctx.channel.send("hello")
@bot.command (name="elsewhere", description="Gives acess to the #elsewhere channel")
async def elsewhere(ctx):
role = discord.utils.get(ctx.guild.roles, name="Elsewhere")
user = ctx.message.author
if role in user.roles:
await user.remove_roles(role)
if role not in user.roles:
await user.add_roles(role)
将member: FetchMember
更改为member: discord.Member
解决了。 .Member 需要大写。
那是因为用户对象不能有特定的公会 - 用户意味着不再在公会中的人。 将async def takevoice(ctx, member: FetchMember):
更改为async def takevoice(ctx, member: discordMember):
,问题将得到解决。这样,您将获得一个 Member 而不是 User 对象,并且会有一个特定的公会。但是(我不知道这是否重要)你不能再对不在公会的人执行此操作。
我正在开发自定义 discord 机器人,主要是为了好玩。我从另一台服务器上找到了另一个名为 Kurisu 的 discord 机器人,它是完全自定义和开源的。这是 github: https://github.com/nh-server/Kurisu. In mod.py (https://github.com/nh-server/Kurisu/blob/port/cogs/mod.py),在第 432 行,有一个功能基本上为用户提供了 No-Help 角色(在服务器中这限制了对特定频道的访问)。我正在尝试做类似的事情,其中具有无语音角色会限制您对语音频道的访问。我正在使用他们的一些代码,即在函数的参数中。我正在执行 async def takevoice(ctx, member: FetchMember): 作为函数。完整功能在这里:
@bot.command(name="takevoice", pass_context=True, description="Removes access to voice channels. Admin only.")
@commands.has_role("Admin") # This must be exactly the name of the appropriate role
async def takevoice(ctx, member: FetchMember):
role = get(member.guild.roles, name="No-Voice")
await member.add_roles(role)
我程序中的其他函数运行良好,但每当我尝试 运行 此函数以另一个用户为目标时,它都不起作用并给我一个回溯错误。它的结尾是:AttributeError:“用户”对象没有属性“公会”。我到处寻找答案,但找不到。任何帮助将不胜感激。
谢谢!
我的机器人的完整代码在这里:(我正在使用其他几个文件,所有这些都可以在 Kurisu github 的 utils 文件夹中找到。也就是说,我正在使用 checks.py、converters.py、database.py、manager.py 和 utils.py。我还使用了一个名为 keep_alive.py 的文件,因为我正在 运行 宁这个在 repl.it.)
import asyncio
import aiohttp
import json
import keep_alive
from discord import Game
from discord.ext.commands import Bot
import datetime
import re
import time
from subprocess import call
import discord
from discord.ext import commands
from checks import is_staff, check_staff_id, check_bot_or_staff
from database import DatabaseCog
from converters import SafeMember, FetchMember
import utils
from discord.ext import commands
from discord.utils import get
TOKEN = "" # Get at discordapp.com/developers/applications/me
bot=commands.Bot(command_prefix=".")
client=discord.Client()
@bot.command(name="hi")
async def hi(ctx):
await ctx.channel.send("hello")
@bot.command (name="elsewhere", description="Gives acess to the #elsewhere channel")
async def elsewhere(ctx):
role = discord.utils.get(ctx.guild.roles, name="Elsewhere")
user = ctx.message.author
if role in user.roles:
await user.remove_roles(role)
if role not in user.roles:
await user.add_roles(role)
将member: FetchMember
更改为member: discord.Member
解决了。 .Member 需要大写。
那是因为用户对象不能有特定的公会 - 用户意味着不再在公会中的人。 将async def takevoice(ctx, member: FetchMember):
更改为async def takevoice(ctx, member: discordMember):
,问题将得到解决。这样,您将获得一个 Member 而不是 User 对象,并且会有一个特定的公会。但是(我不知道这是否重要)你不能再对不在公会的人执行此操作。