使用Python在400空消息代码中打印变量输出

时间:2019-03-08 20:31:46

标签: python discord discord.py bad-request

问题:

我正在尝试为Discord编写机器人程序,以便获取变量并将其作为消息发送。例如,“ a”设置为42,并且我希望该机器人在聊天室中打印“数字为42 {作者姓名}”:

a = 32

if message.content.startswith('!gap'):
    msg = print('a'.format(message))
    await client.send_message(message.channel, msg)

错误:

Ignoring exception in on_message
Traceback (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 个答案:

答案 0 :(得分:1)

您希望msg是一个字符串,但是如果尝试使用它:

>>> msg = print("anything at all")
>>> repr(msg)
None

只需删除print通话即可。

答案 1 :(得分:1)

您不应使用on_message事件发出命令。使用内置的命令处理程序代替,可以提高程序设计和效率。

以下代码输出您在!gap

之后键入的数字
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的重写分支,该分支具有很多改进。