python-3.x - 使用 discord.py 向指定 channel 发送消息时出错

标签 python-3.x typeerror discord.py-rewrite

当有人加入我的不和谐时,我在尝试向指定 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/

相关文章:

Python csv.DictReader 内存不足

python - 如何使用 python 生成带有节点列表的 xml

python - 如何在Python 3中重用父类(super class)方法的变量?

python - 尝试在 Python 3 中拆分字符串,得到 'str' 不支持缓冲区接口(interface)

javascript - req.send ('null' ) 不是 Node.js mySQl 的函数

当搜索字段为空时,Django SearchFilter 显示所有数据

javascript TypeError : document. getElementsByTagName ("p")[0].innerHtml 不是函数

python - Discord.py 重写 get_member() 函数为除 bot 之外的所有用户返回 None

python - 是否可以使用不带齿轮的 discord.py 进行面向对象编程?

python - 如何通过BucketType定义命令错误消息