当有人加入我的不和谐时,我在尝试向指定 channel 发送消息时遇到问题。
我收到此错误:
Ignoring exception in on_member_join
Traceback (most recent call last):
File "C:\Users\Timo\Anaconda3\envs\Discord\lib\site-packages\discord\client.py", line 270, in _run_event
await coro(*args, **kwargs)
File "C:/Users/Timo/PycharmProjects/Discord/Bot 1/Main.py", line 30, in on_member_join
channel = Bot.get_channel("649275309396590626")
TypeError: get_channel() missing 1 required positional argument: 'id'
我使用的是 v1.2.5 版本。
这是代码:
@client.event
async def on_member_join(member):
channel = Bot.get_channel("649275309396590626")
await channel.send(f"{member} ist dem Server beigetreten!")
这是 Bot 变量:
import discord
from discord.ext import commands
from discord.ext.commands import Bot
client = Bot(command_prefix=".")
我将其编辑为:
@client.event async def on_member_join(member): channel = client.get_channel("649275309396590626") await channel.send(f"{member} ist dem Server beigetreten!")
现在我有这个错误:
File "C:/Users/Timo/PycharmProjects/Discord/Bot 1/Main.py", line 31, in on_member_join await channel.send(f"{member} ist dem Server beigetreten!") AttributeError: 'NoneType' object has no attribute 'send'
最佳答案
client.get_guild(id) 函数需要整数输入。当这没有正确给出或 id 无效时,该函数将返回一个 None 值。当您尝试使用 NoneType 对象发送某些内容时,会导致错误。由于 NoneType 对象没有“发送”功能。
我建议阅读文档:https://discordpy.readthedocs.io/en/latest/api.html?highlight=get_guild#discord.Client.get_guild
关于python-3.x - 使用 discord.py 向指定 channel 发送消息时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59522717/