【问题标题】:How to fix this error discord python?如何修复此错误不和谐 python?
【发布时间】:2018-07-06 07:36:11
【问题描述】:
if message.content.lower().startswith('!kick') and (roleLFJob in message.author.roles or roleLFAba in message.author.roles):
    await client.delete_message(message)
    serverchannel = '405090256124248065'
    messageParsed = message.content.split()
    kick = messageParsed[0]
    mention = messageParsed[1]
    msg = messageParsed[2:]
    for member in message.mentions:
        await client.kick(member)
        await client.send_message(discord.Object(id=serverchannel), '{0} was kicked by {1}, with reason:"**'.format(member.mention, message.author.mention) + msg + '**"')

当我不和谐地写这个命令时:

!kick @(member.mention) reason, reason and reason

出现此错误:

Ignoring exception in on_message Traceback (most recent call last):  
File "C:\Users\senuk\AppData\Local\Programs\Python\Python35\lib\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)   
File "overmind.py", line 128, in on_message
    await client.send_message(discord.Object(id=serverchannel), '{0} was kicked by {1} with reason:"**'.format(member.mention, message.author.mention) + msg + '**"') 
TypeError: Can't convert 'list' object to str implicitly

【问题讨论】:

  • 在格式字符串中使用 str(msg) 代替 msg

标签: python discord discord.py


【解决方案1】:

既然你输入了这个:

!kick @(member.mention) reason, reason and reason

那么当你拆分整个内容时:

messageParsed = message.content.split()
kick = messageParsed[0]
mention = messageParsed[1]
msg = messageParsed[2:]

结果将是:

kick = "!kick"
mention = "@(member.mention)"
msg = ["reason,", "reason", "and", "reason"]

在代码的最后一行,您尝试使用 msg(列表)连接两个字符串。这是不可能的。

你可能想要msg 是一个字符串,因此你应该使用:

msg = " ".join(messageParsed[2:])

msg 重新制作回原来的形式:

msg = "reason, reason and reason"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 2021-08-14
    • 2011-02-20
    • 2021-01-13
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    相关资源
    最近更新 更多