我一直在研究用 python 編碼的不和諧機器人。當用戶加入服務器并離開服務器時,我一直試圖讓我的機器人說出一條消息。但是當我測試它時,我得到了 2 個錯誤,我不明白這些錯誤。而且我不確定我做錯了什么。如果有人可以幫助解釋這些錯誤,并為我的代碼指出正確的方向,我將不勝感激。我研究了discord rewrite api,并與朋友在我的服務器中對其進行了測試,當時我遇到了錯誤@client.eventasync 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.eventasync 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 givendiscord.errors.InvalidArgument: Destination must be Channel, PrivateChannel, User, or Object. Received NoneType這是我的完整追溯Ignoring exception in on_member_joinTraceback (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 givenIgnoring exception in on_member_removeTraceback (most recent call last):
2 回答

慕無忌1623718
TA貢獻1744條經驗 獲得超4個贊
您的代碼的一種簡單修復方法是使用 ,而不是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)
返回具有以下 ID 的 abc.GuildChannel 或 abc.PrivateChannel。
如果未找到,則返回 None。
所以你沒有成功選擇頻道。原因是 rewrite 中的 ID 是int,而不是字符串。

慕雪6442864
TA貢獻1812條經驗 獲得超5個贊
member.server
向您返回成員加入的服務器。并且您嘗試使用 ID 訪問頻道499457708978864151
,這肯定不會在頻道使用 ID 所在的服務器之外的其他服務器中工作499457708978864151
,因此您也必須讓服務器client.get_server("ID")
執行此操作。
添加回答
舉報
0/150
提交
取消