【问题标题】:Cloning and Deleting a channel: Discord.py克隆和删除频道:Discord.py
【发布时间】:2021-01-25 05:24:32
【问题描述】:
@client.command()
@commands.is_owner()
async def nuke(ctx, channel_name):
    existing_channel = discord.utils.get(guild.channels, name=channel_name)
    if existing_channel is not None:
        await clone(name=channel_name,reason="Has been nuked")
        await existing_channel.delete()
    else:
        await ctx.send(f'No channel named **{channel_name}** was found')

我在 discord 上看到多个机器人已经可以通过 nuke 命令执行此操作,但我想自己学习如何执行此操作。问题是,我的机器人无法检测到我提到了一个频道(如附图所示)。我发现了一个类似的问题,关于“正确地核对一个频道”,但他们使用的是 cogs。帮助? This is the attached picture

【问题讨论】:

  • 嗨。你想通过channel_name 传递什么?当您标记频道 #example-channel 并且它是指向该频道的可点击链接时,它实际上使用频道 ID 并且将是 。您的搜索将找不到。您必须输入 nuke example-channel 进行搜索才能找到任何内容
  • 我遇到的问题是,无论我输入什么,我仍然不会收到错误消息????我曾尝试使用nuke example-channel,但最终还是没有用。有没有办法让机器人改用可点击的#example-channel?

标签: python discord discord.py


【解决方案1】:

你用错了clone(),它的Channel.clone()不仅仅是clone()

以下是修改后的代码

@client.command()
@commands.is_owner()
async def nuke(ctx, channel_name):
    existing_channel = discord.utils.get(guild.channels, name=channel_name)
    if existing_channel is not None:
        await existing_channel.clone(reason="Has been nuked")
        await existing_channel.delete()
    else:
        await ctx.send(f'No channel named **{channel_name}** was found')

【讨论】:

  • 我发现这确实克隆了频道,但没有删除频道。同时我会看看这背后是否有任何原因。
【解决方案2】:

只是为了好玩已经指出您一直在使用错误的命令进行克隆。 我正在使用他们的代码,但是在您希望频道名称成为可点击的#example-channel 的位置进行更改。

标记频道时,机器人会看到:<#123456789>,而不是 #example-channel。我们只需要数字,因此我们可以获得频道 ID。前两行代码。

@client.command()
@commands.is_owner()
async def nuke(ctx, channel_name):
    channel_id = int(''.join(i for i in channel_name if i.isdigit())) 
    existing_channel = client.get_channel(channel_id)
    if existing_channel:
        await existing_channel.clone(reason="Has been nuked")
        await existing_channel.delete()
    else:
        await ctx.send(f'No channel named **{channel_name}** was found')


尝试调试时的一个好主意是print() 不同的对象,这样您就可以看到它们是什么。

print(channel_name) 将向您显示机器人在您标记时看到的内容。您或许可以从中看出为什么它没有找到任何“频道名称”。

【讨论】:

  • if existing_channel is not None: 可以缩短为 if existing_channel:
  • @kr8gz 真的!感谢您指出了这一点。现在在代码中更改它。
猜你喜欢
  • 1970-01-01
  • 2021-04-27
  • 1970-01-01
  • 2021-11-08
  • 2015-08-08
  • 2021-10-04
  • 2021-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多