問題:我正在嘗試為 Discord 編寫一個機器人,以便獲取一個變量并將其作為消息發(fā)送。例如,“a”設(shè)置為 42,我希望機器人在聊天中打印“The number is 42 {author name}”:a = 32if message.content.startswith('!gap'): msg = print('a'.format(message)) await client.send_message(message.channel, msg)錯誤:Ignoring exception in on_messageTraceback (most recent call last): File "C:\Users\trevo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\client.py", line 307, in _run_event yield from getattr(self, event)(*args, **kwargs) File "C:\Users\trevo\Desktop\dcbot\reply.py", line 16, in on_message await client.send_message(message.channel, msg) File "C:\Users\trevo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\client.py", line 1152, in send_message data = yield from self.http.send_message(channel_id, content, guild_id=guild_id, tts=tts, embed=embed) File "C:\Users\trevo\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\http.py", line 200, in request raise HTTPException(r, data)discord.errors.HTTPException: BAD REQUEST (status code: 400): Cannot send an empty message我很欣賞解釋!
2 回答

狐的傳說
TA貢獻1804條經(jīng)驗 獲得超3個贊
您希望msg是一個字符串,但如果您嘗試:
>>> msg = print("anything at all")
>>> repr(msg)
None
只需刪除print呼叫。

jeck貓
TA貢獻1909條經(jīng)驗 獲得超7個贊
您不應(yīng)該使用 on_message 事件來發(fā)出命令。改用內(nèi)置命令處理程序,這樣可以提高程序設(shè)計和效率。
以下代碼輸出您在!gap后鍵入的數(shù)字
from discord.ext import commands
client = commands.Bot(command_prefix='!')
@client.command(pass_context=True)
async def gap(ctx, number):
await client.say(f"{ctx.author.mention} said the number {number}")
client.run("token")
我還建議查看 API 的重寫分支,它有很多改進。
添加回答
舉報
0/150
提交
取消