【问题标题】:How to change channel permissions without having a message sent first?如何在不先发送消息的情况下更改频道权限?
【发布时间】:2021-11-23 23:17:12
【问题描述】:

我有一个程序可以更改频道成员在一天中的特定时间可以看到的内容。为此,我可以更改每个成员的角色,或者更改每个频道的权限。但是,我已经查看了整个网络,并且任何一种方法的所有方式都需要发送一条消息,以便可以读取来自该消息的数据并将其放入函数中,例如:

@client.command()
async def perm(ctx):
    await ctx.channel.set_permissions(ctx.guild.default_role, send_messages=False

Change the permissions of a discord text channel with discord.py

async def addrole(ctx):
    member = ctx.message.author
    role = get(member.server.roles, name="Test")
    await bot.add_roles(member, role)

Discord.py | add role to someone

我当前的程序如下所示:

import discord
client = discord.Client()
import datetime

async def live_day(schedule):
  current_place = "the void"
  while True:
    current_time = str((datetime.datetime.now() -datetime.timedelta(hours=7)).time())
    int_time = int(current_time[:2] + current_time[3:5])
    for x in range(len(schedule)):
      try:
        start_time = schedule[x][1]
        end_time = schedule[x + 1][1]
      except IndexError:
        end_time = 2400
      if current_place != schedule[x][0] and int_time >= start_time and int_time < end_time:
        current_place = schedule[x][0]
        #Change the channel permissions of the current place to allow viewing
        #Change the channel permissions of the last channel to disallow viewing


@client.event
async def on_ready():
  print("{0.user} has arrived for duty".format(client))
  client.loop.create_task(live_day([
    ("Home", 0), 
    ("Work", 900), 
    ("Home", 1700), 
    ("Nightclub", 2000), 
    ("Home", 2200),
    ]))


client.run(my_secret)

别介意写得不好的代码,我该怎么做,或者我应该去哪里解决这个问题?任何帮助表示赞赏。谢谢!

编辑:我可以使用这个单独获取频道,

channel1=client.get_channel(channelid)

discord.py, send message by channel id without on_message() event?

但是我不能将它用于多个服务器。如何按名称获取频道?

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    注意on_ready()

    • 这个函数不能保证是第一个调用的事件。同样,这个函数也不能保证只被调用一次。该库实现了重新连接逻辑,因此每当 RESUME 请求失败时都会调用此事件。

    • 使用此功能可能会导致机器人崩溃。

    改为创建background tasks 并使用wait_until_ready()

    您可以在https://github.com/Rapptz/discord.py/blob/v1.7.3/examples/background_task.py中找到示例

    【讨论】:

      【解决方案2】:

      如果您只想更改一个频道的权限(每个公会),这可能符合您的需求:

      #changing "Home" channel visibility, every day at 5 pm.
      
      @client.event
      async def on_ready():
          while True:
              await asyncio.sleep(1)
              current_time = datetime.datetime.now()
              five_pm = current_time.replace(hour=17, minute=0, second=0)
              if current_time == five_pm:
                  for guild in client.guilds: #looping through all the guilds your bot is in
                      channel = discord.utils.get(client.get_all_channels(), name="Home") #getting channel by name by using "discord.utils"
                      await channel.set_permissions(guild.default_role, view_channel=True) #changing permissions
      

      通过使用on_ready() 事件,您可以在不发送任何消息的情况下循环时间检查

      记得import discord.utils

      【讨论】:

        猜你喜欢
        • 2017-06-01
        • 2021-07-01
        • 2015-08-19
        • 1970-01-01
        • 2021-07-30
        • 2021-08-04
        • 2023-03-26
        • 2019-09-26
        • 1970-01-01
        相关资源
        最近更新 更多