python-3.x - 使用 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'
解决方案
client.get_guild(id) 函数需要一个整数输入。如果未正确给出或 id 无效,该函数将返回 None 值。当您尝试使用 NoneType 对象发送内容时,会导致错误。由于 NoneType 对象没有“发送”功能。
我建议阅读文档:https ://discordpy.readthedocs.io/en/latest/api.html?highlight=get_guild#discord.Client.get_guild
推荐阅读
- ansible - 无法执行服务重启,在ansible中从非root用户复制文件
- javascript - 粘贴到没有特殊字符的文本区域
- java - wrap_content 如何与编辑文本视图一起使用?
- java - Firestore 不查询第二个集合
- c# - 将十进制转换为十六进制
- c++ - GSL 的 Expects(cond) 对运行时的性能影响是什么?
- angular - 在 Observable.interval() 中捕获 http 错误
- sql-server - 如何从记录集中获取 ID 字段?
- netlogo - 如何针对以下情况做出一些行为?
- javascript - 如何在移动设备上禁用 jQuery 自动完成小部件的悬停效果