How would I get every text and voice channel and denying @ everyone to read it, and then allowing another role to read and send?

喜欢而已 提交于 2020-02-06 08:37:28

问题


I wondered how when the bot is added to a guild, I can make it get every text and voice channel and then deny @ everyone access to read it, but then allow another role called 'Verified' to read it?
I am using the rewrite version of Discord.py

EDIT: I found how to change the permissions using

await message.channel.set_permissions(message.author, read_messages=True, send_messages=False)

But I still don't know how to apply this on every channel


回答1:


Posting this here for others, even though you've gotten this answered on the Discord ;)

You'd need to iterate through each channel in a guild via Guild.channels




回答2:


This will only change things for the user who sent the message. To block everyone, you must set permissions for Guild.default_role instead. The below command accepts an existing role and any number of members. It gives the invoker and all those members that role, and then disables reading messages for everyone without that role.

from discord.ext import commands
import discord

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

@bot.command()
async def verify(ctx, role: discord.Role, *members: discord.Member):
    for member in (ctx.author, *members):
        await member.add_roles(role, reason=f"Verify command by {ctx.author.id}")
    for channel in ctx.guild.channels:
        await channel.set_permissions(ctx.guild.default_role, read_messages=False)
        await channel.set_permissions(role, read_messages=True)

bot.run("Token")


来源:https://stackoverflow.com/questions/52411903/how-would-i-get-every-text-and-voice-channel-and-denying-everyone-to-read-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!