【问题标题】:Can't delete discord channel using its name as the variable for the name is causing nonetype error无法使用其名称作为名称的变量删除不和谐频道导致非类型错误
【发布时间】:2020-09-18 06:00:44
【问题描述】:

我有一个命令,它接受一个输入,然后根据该输入为票务系统删除一个频道。该命令现在可以删除通道,但删除方法无法识别“输出”变量。

我是不和谐的新手,提前抱歉。

if message.content.startswith(".close"):
        verify_channel = client.get_channel(756238816511131773)
        if not message.channel.id == verify_channel.id:
            await message.delete()
        else:
            msg = message.content.split()
            output = ""
            for word in msg[1:]:
                output += word
                output += " "
        
       guild = message.guild
            channel = discord.utils.get(guild.text_channels, name=output)
            await channel.delete()

运行命令时出错:

AttributeError: 'NoneType' object has no attribute 'delete'

【问题讨论】:

  • 几乎但是通道名称由变量“输出”确定,新问题似乎是“输出”在搜索通道名称时不起作用,并且每个命令都有不同的通道名称时间,所以我不能使用默认频道名称
  • 然后将你的问题编辑成你真正想要的,因为你问的是is there a method of getting the channel ID from the name of the channel,如果你有其他与这个问题无关的问题,请创建一个新问题。
  • 我更新了问题,你
  • 您的原始代码/问题中存在问题,我在编写答案时没有注意到,但在 else 语句之前未声明输出,因此它将超出范围,因此 @987654326 @ 到 discord.utils.get() 被调用时。该函数将找不到None通道并返回None

标签: python discord discord.py discord.py-rewrite


【解决方案1】:

这是使用频道提及的工作代码。聊天中的提及如下所示:I'm mentioning a channel: #ghi 其中ghi 是频道名称。如果您打印出来,它会将#ghi 转换为<#622194268139683870>。您可以使用正则表达式或其他方法提取它,但您也可以在消息上调用函数:

channel = message.channel_mentions[0] #Only use the first channel mentioned
await channel.delete()

这是一个转换为命令的工作示例,我建议使用 if startswith(command_name) 启动每个函数

bot = commands.Bot(command_prefix=".")

@bot.command()
async def close(ctx):
    channel = ctx.message.channel_mentions[0]
    await channel.delete()
    print("Success")

然后您可以从聊天中调用,例如 .close #ghi

如果您打算保留它,请确保将其添加到 ON_MESSAGE 事件的顶部

async def on_message(message):
    await bot.process_commands(message)

我已经测试过了,它可以工作。


PS: 命令还具有轻松获取参数而不是用空格分割字符串的优点,我现在无法解释,但我推荐文档:https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html

我没有包括这个,因为你的函数可以在没有它的情况下编写。

【讨论】:

  • 这是一个更好的解决方案,我认为它会起作用,但是当我在聊天中运行 .close #ticket-xxxx 时没有任何反应,控制台似乎甚至无法识别它正在运行
  • 我刚刚编辑了我的答案,再次检查粗体句下的部分。它涉及添加到您的 on_message 事件的特殊行。
  • 您好,我看了一下,但我更喜欢命令选项,它更简单,所以我尝试删除 on_message 版本,但命令版本似乎没有做任何事情?
  • @max 您的on_message 正在阻止该命令。要么删除它,要么在on_message 下添加await bot.process_commands(message),就像@Allister 提到的那样。
  • 是的,我完全删除了 on_message 版本
【解决方案2】:

您可以使用get通过名称获取通道对象

@bot.event
async def on_message(message):
    if message.content.startswith(".close"):
        guild = message.guild
        channel = discord.utils.get(guild.text_channels, name="channel_name")
        await channel.delete()

【讨论】:

  • 这个概念似乎可以工作,但是当我将其更改为输出时,“channel_name”一直返回 None(另一个变量应该是通道名称)。 AttributeError: 'NoneType' object has no attribute 'delete'channel = discord.utils.get(guild.text_channels, name=output)
  • 您在售票频道中使用的是.close吗?我假设这是关闭工单的命令,并且通过关闭工单您试图删除频道?
  • .close 没有在票务通道中使用它本身,因为命令必须有一个过滤器以确保它没有在任何其他通道中使用(例如一般的成员),因此它具有特定的频道只能用在
  • print(output) 返回什么?
  • 它返回“ticket-4393”,这是我输入的文本通道。整个命令将是“.close ticket-4393”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-22
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 2013-11-29
  • 2020-09-18
相关资源
最近更新 更多