【问题标题】:How can I stop a specific role from talking in discord using discord.py如何使用 discord.py 阻止特定角色在不和谐中说话
【发布时间】:2021-08-07 00:37:33
【问题描述】:

所以,我想做的是使用 discord.py 阻止特定角色在不和谐频道中发言。

这是我目前所拥有的。

import discord
import os
from keep_alive import keep_alive

client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
  countingBotGuildSave = ['test']
  if any(word in message.content for word in countingBotGuildSave):
    await message.channel.set_permissions(discord.utils.get(message.guild.members, name='Foo'), send_messages=False)
    await message.channel.send('**An admin/moderator has locked this channel. Please wait for an admin to unlock this channel with `+unlock`.**')
    print(f'{message.author} locked channel {message.channel}')

keep_alive()
client.run(os.getenv('TOKEN'))

这在我运行机器人时不会导致任何错误,但是当我不和谐地说test 时,它说:目标参数必须是成员或角色。我不明白,因为我做了一个名为 Foo 的角色。

你能帮忙吗?

Tysm。

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    错误本身会告诉您出了什么问题。在这种情况下,您的 await 函数没有任何意义。您正在寻找名称为 Fooguild.members,但您想要的是 role

    看看下面的代码:

    @client.event
    async def on_message(message):
      countingBotGuildSave = ['test']
      if any(word in message.content for word in countingBotGuildSave):
        await message.channel.set_permissions(discord.utils.get(message.guild.roles, name='Foo'), send_messages=False) # Get the role
        await message.channel.send('**An admin/moderator has locked this channel. Please wait for an admin to unlock this channel with `+unlock`.**')
        print(f'{message.author} locked channel {message.channel}')
    

    在这里,我们在名为Fooguild 中寻找role。您也可以只使用 ID 将名称更改为您想要的任何名称,而无需编辑代码 (id=RoleID)。

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-11
      • 1970-01-01
      • 2020-12-05
      • 2021-03-05
      • 2020-08-20
      • 1970-01-01
      • 2021-06-11
      • 2021-08-23
      相关资源
      最近更新 更多