因此,当前我有一个不和谐的Bot,我想让它欢迎#join中的任何新用户,并为他们提供昵称“ [0]成员名称”。我没有收到任何错误,但是这些功能都不起作用!
编辑:重新编写了一些代码,我现在遇到此错误:
编辑2:仍然无法编辑昵称,但是当用户离开服务器时,我从函数中收到这些错误,以检查用户是否为职员。我没有从message.author中得到错误,但是当消息作者离开时,我开始收到此错误。当有人离开服务器时,我尝试重置message.author,但这无济于事!我对如何停止这些错误一无所知!
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Evan\Anaconda3\envs\testing\lib\site-packages\discord\client.py", line 270, in _run_event
await coro(*args, **kwargs)
File "C:/Users/Evan/PycharmProjects/Bot/bot.py", line 107, in on_message
top_role = message.author.top_role
AttributeError: 'User' object has no attribute 'top_role'
我的新编辑代码
import discord
import asyncio
import time
# id = 630529690792230923
messages = joined = 0
client = discord.Client()
@client.event
async def on_ready():
print(f'Logged in as: {client.user.name}')
print(f"User ID: {client.user.id}")
print('-----')
async def update_stats():
await client.wait_until_ready()
global messages, joined
while not client.is_closed():
try:
with open("stats.txt", "a") as f:
f.write(f"Time: {int(time.time())}, Messages: {messages}, Members Joined: {joined}\n"),
messages = 0
joined - 0
await asyncio.sleep(3600)
except Exception as e:
print(e)
await asyncio.sleep(3600)
@client.event
async def on_raw_reaction_add(payload):
message_id = payload.message_id
if message_id == 638155786559684608:
guild_id = payload.guild_id
guild = discord.utils.find(lambda g: g.id == guild_id, client.guilds)
if payload.emoji.name == 'Test':
role = discord.utils.get(guild.roles, name='new role')
else:
role = discord.utils.get(guild.roles, name=payload.emoji.name)
if role is not None:
member = discord.utils.find(lambda m: m.id == payload.user_id, guild.members)
if member is not None:
await member.add_roles(role)
print("done")
else:
print("Member Not Found")
@client.event
async def on_raw_reaction_remove(payload):
message_id = payload.message_id
if message_id == 638155786559684608:
guild_id = payload.guild_id
guild = discord.utils.find(lambda g: g.id == guild_id, client.guilds)
if payload.emoji.name == 'Test':
role = discord.utils.get(guild.roles, name='new role')
else:
role = discord.utils.get(guild.roles, name=payload.emoji.name)
if role is not None:
member = discord.utils.find(lambda m: m.id == payload.user_id, guild.members)
if member is not None:
await member.remove_roles(role)
print("done")
@client.event
async def on_member_join(member):
global joined
joined += 1
rule_channel = member.guild.get_channel(channel_id=630530486858547223)
newusermessage = f"""Welcome to CB:NL {member.mention}! Have a Great Time And Make Sure to Look At {rule_channel}"""
channel = member.guild.get_channel(channel_id=630563931412496434)
role = member.guild.get_role(role_id=630533613947060244)
if member is not None:
await member.add_roles(role)
print("done")
await member.edit(str([f"[0] {member.display_name}"]))
await channel.send(newusermessage)
@client.event
async def on_member_remove(member):
discord.message.author = member
@client.event
async def on_message(message):
global messages
messages += 1
id = client.get_guild(630529690792230923)
bad_words = ["test"]
channels = ["bot-commands", "staff-general"]
pn = 1
author = message.author
top_role = message.author.top_role
staff_role = message.author.guild.get_role(role_id=630532857655328768)
if top_role > staff_role:
if message.content.startswith == "-clean":
pass
if str(message.channel) in channels:
if message.content.find("-hello") != -1:
await message.channel.send("Hi")
elif message.content == "-status":
await message.channel.send(f"""# of Members: {id.member_count}""")
else:
if message.author.bot is not 1:
print(f"""{message.author} tried to do command {message.content}""")
await message.channel.send(f"Error, {message.author.mention}. You Cannot Do That!")
client.loop.create_task(update_stats())
答案 0 :(得分:1)
我会回答,因为您的代码中存在多个错误,并且很难在注释中添加。如果遇到其他错误,可以在这篇文章下发表评论。
role = discord.utils.get(discord.Guild.roles, name="Member")
该错误是您按成员名称检索角色的地方。最好通过ID来获取角色,您可以使用member.guild.get_role(630533613947060244)
来实现。错误是discord.Guild.roles
不是可迭代的属性。
nick = discord.utils.get(str(member.nick))
不确定那里的目的是什么,您可以使用nick = member.nick
来获取带有成员昵称的字符串。
要编辑昵称,您应该使用:
await member.edit(nick=f"[0] {member.display_name}")
AttributeError:“用户”对象没有属性“ top_role”
出现此错误是因为您想访问top_role
实例上的discord.User
属性,但是只有discord.Member
定义了此属性。当有人离开服务器时,您将获得User实例而不是Member实例。
if isinstance(message.author, discord.Member):
top_role = message.author.top_role
else:
top_role = None # top role not available, user has no roles