【问题标题】:How can I stop a specific role from speaking in discord.py?如何阻止特定角色在 discord.py 中发言?
【发布时间】:2021-05-10 01:08:30
【问题描述】:

所以,现在我正在尝试让特定角色无法在频道中讲话。我有有效的代码,但只限制ctx.guild.default_role(@everyone) 说话。

import discord
import os
from discord.ext import commands
from keep_alive import keep_alive
import asyncio

bot = commands.Bot(command_prefix='+')

@bot.event
async def on_ready():
    print(f'''We have logged in as {bot.user.name}''')
  
@bot.command()
@commands.guild_only()
@commands.has_guild_permissions(manage_channels=True)
@commands.bot_has_guild_permissions(manage_channels=True)
async def lock(ctx):
    await ctx.channel.set_permissions(ctx.guild.default_role, send_messages=False)
    await ctx.send('**An admin/moderator has locked this channel. Please wait for an admin to unlock this channel with `+unlock`.**')
    print(f'{ctx.author} locked channel {ctx.channel}')

@bot.command()
@commands.guild_only()
@commands.has_guild_permissions(manage_channels=True)
@commands.bot_has_guild_permissions(manage_channels=True)
async def unlock(ctx):
    await ctx.channel.set_permissions(ctx.guild.default_role, send_messages=True)
    await ctx.send('**An admin/moderator has unlocked this channel with `unlock`.**')
    print(f'{ctx.author} unlocked channel {ctx.channel}.')

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

有人可以帮帮我吗?

重述问题:我需要像“农民角色”这样不能说话的角色,而当管理员说话时,@everyone 角色仍然可以在该频道中说话。

谢谢大家:)

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    如下使用TextChannel.set_permissionsdiscord.utils.get

    await ctx.channel.set_permissions(discord.utils.get(ctx.guild.members, name='Foo'), send_messages=False)
    

    discord.utils.get(ctx.guild.members, name='Foo') 按名称获取不和谐角色。

    【讨论】:

      猜你喜欢
      • 2021-08-07
      • 2021-08-15
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 2021-06-29
      • 2020-11-27
      • 2018-08-27
      • 2020-01-31
      相关资源
      最近更新 更多