【发布时间】:2021-06-01 06:37:05
【问题描述】:
所以,我尝试制作一个机器人,每次用户加入我的服务器时都会将嵌入发送到特定频道。 代码是这样的
import discord
import asyncio
import datetime
from discord.ext import commands
intents = discord.Intents()
intents.members = True
intents.messages = True
intents.presences = True
bot = commands.Bot(command_prefix="a!", intents=intents)
@bot.event
async def on_ready():
print('Bot is ready.')
@bot.event
async def on_member_join(ctx, member):
embed = discord.Embed(colour=0x1abc9c, description=f"Welcome {member.name} to {member.guild.name}!")
embed.set_thumbnail(url=f"{member.avatar_url}")
embed.set_author(name=member.name, icon_url=member.avatar_url)
embed.timestamp = datetime.datetime.utcnow()
channel = guild.get_channel(816353040482566164)
await channel.send(embed=embed)
我遇到了一个错误
Ignoring exception in on_member_join
Traceback (most recent call last):
File "C:\Users\Piero\AppData\Roaming\Python\Python39\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:\Users\Piero\Documents\Discord\a-chan\achan_bot\main.py", line 24, in on_member_join
channel = guild.get_channel(816353040482566164)
NameError: name 'guild' is not defined
有人知道我的代码有什么问题吗?
【问题讨论】:
-
您没有在代码中的任何地方定义
guild,那么 Python 应该如何知道那是什么? -
我不认为
ctx是传递给on_member_join的参数。看documentation,好像只有member通过了。
标签: python-3.x discord.py