gpt4 book ai didi

python-3.x - 不和谐的欢迎机器人

转载 作者:行者123 更新时间:2023-12-04 08:01:31 25 4
gpt4 key购买 nike

因此,我尝试制作一个机器人,每次用户加入我的服务器时都会将其嵌入到特定 channel 。
代码看起来像这样

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
有人知道我的代码有什么问题吗?

最佳答案

首先来看 discord.py documention , ctx没有传递给 on_member_join事件引用。但是,您可以使用 attributes of member 这是为了获得您需要的值而传递的。

@bot.event
async def on_member_join(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 = member.guild.get_channel(816353040482566164)
await channel.send(embed=embed)
有趣的是,你这样做是为了获得公会名称,但似乎你在检索 channel 时忘记这样做了。 .

关于python-3.x - 不和谐的欢迎机器人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66444121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com