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

问题:

我正在尝试为 Discord 编写一个机器人,以便获取一个变量并将其作为消息发送。例如,“a”设置为 42,我希望机器人在聊天中打印“The number is 42 {author name}”:


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

我很欣赏解释!


泛舟湖上清波郎朗
浏览 330回答 2
2回答

狐的传说

您希望msg是一个字符串,但如果您尝试:>>> msg = print("anything at all")>>> repr(msg)None只需删除print呼叫。

jeck猫

您不应该使用 on_message 事件来发出命令。改用内置命令处理程序,这样可以提高程序设计和效率。以下代码输出您在!gap后键入的数字from discord.ext import commandsclient = 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 的重写分支,它有很多改进。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python