我一直在研究用python编码的discord机器人。而且我一直在尝试让我的机器人在用户加入服务器并离开服务器时说一条消息。但是当我测试它时,我遇到了2个错误,这些错误我不理解。而且我不确定我做错了什么。如果有人可以帮助解释这些错误,并为我的代码指出正确的方向,我将不胜感激。
我调查了不和谐的重写api,并在我遇到错误的时候与一个朋友在服务器中对其进行了测试
@client.event
async def on_member_join(member):
channel = member.server.get_channel("499457708978864151")
msg = 'Welcome to the {1.name} Server, {0.mention}'
await client.say(channel, msg.format(member, member.server))
@client.event
async def on_member_remove(member):
channel = member.server.get_channel("499457708978864151")
msg = '{0.mention} has left the server'
await client.say((channel, msg.format(member, member.server)))
我希望我的机器人说“欢迎使用(服务器的名称)服务器”,当他们进入服务器时使用@username,当用户离开时@username离开服务器,但是我收到以下错误消息:
TypeError: send_message() takes from 2 to 3 positional arguments but 4 were given
discord.errors.InvalidArgument: Destination must be Channel, PrivateChannel, User, or Object. Received NoneType
这是我的完整回溯
Ignoring exception in on_member_join
Traceback (most recent call last):
File "C:\Users\Chris\PycharmProjects\untitled1\venv\lib\site-packages\discord\client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "C:/Users/Chris/PycharmProjects/untitled1/RomaniBot.py", line 37, in on_member_join
await client.say(channel, msg.format(member, member.server))
File "C:\Users\Chris\PycharmProjects\untitled1\venv\lib\site-packages\discord\ext\commands\bot.py", line 350, in say
coro = self.send_message(destination, *args, **kwargs)
TypeError: send_message() takes from 2 to 3 positional arguments but 4 were given
Ignoring exception in on_member_remove
Traceback (most recent call last):
File "C:\Users\Chris\PycharmProjects\untitled1\venv\lib\site-packages\discord\client.py", line 307, in _run_event
yield from getattr(self, event)(*args, **kwargs)
File "C:/Users/Chris/PycharmProjects/untitled1/RomaniBot.py", line 43, in on_member_remove
await client.say((channel, msg.format(member, member.server)))
File "C:\Users\Chris\PycharmProjects\untitled1\venv\lib\site-packages\discord\ext\commands\bot.py", line 309, in _augmented_msg
msg = yield from coro
File "C:\Users\Chris\PycharmProjects\untitled1\venv\lib\site-packages\discord\client.py", line 1145, in send_message
channel_id, guild_id = yield from self._resolve_destination(destination)
File "C:\Users\Chris\PycharmProjects\untitled1\venv\lib\site-packages\discord\client.py", line 289, in _resolve_destination
raise InvalidArgument(fmt.format(destination))
discord.errors.InvalidArgument: Destination must be Channel, PrivateChannel, User, or Object. Received NoneType
答案 0 :(得分:1)
一个简单的代码修复方法是使用client.say
而不是使用channel.send
。所以
@client.event
async def on_member_join(member):
channel = member.server.get_channel(499457708978864151)
msg = 'Welcome to the {1.name} Server, {0.mention}'
await channel.send(msg.format(member, member.server))
此外,根据文档
get_channel(id)
使用以下命令返回abc.GuildChannel或abc.PrivateChannel 以下ID。
如果未找到,则返回None。
因此,您没有成功选择频道。原因是重写的ID是int
,而不是字符串。
答案 1 :(得分:0)
member.server
返回您成员所在的服务器。并且您尝试访问ID为499457708978864151
的通道,这肯定不能在ID为499457708978864151
的服务器之外的其他服务器中工作,因此您也必须获得服务器{{1 }}会这样做。