首页 > 解决方案 > 使用 discord.py 向指定频道发送消息时出错

问题描述

当有人加入我的不和谐时,我在尝试向指定频道发送消息时遇到问题。我得到了这个错误:

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!")

这是机器人变量:

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'

标签: python-3.xtypeerrordiscord.py-rewrite

解决方案


client.get_guild(id) 函数需要一个整数输入。如果未正确给出或 id 无效,该函数将返回 None 值。当您尝试使用 NoneType 对象发送内容时,会导致错误。由于 NoneType 对象没有“发送”功能。

我建议阅读文档:https ://discordpy.readthedocs.io/en/latest/api.html?highlight=get_guild#discord.Client.get_guild


推荐阅读