【问题标题】:Discord.py Timeout server membersDiscord.py 超时服务器成员
【发布时间】:2022-01-24 07:03:17
【问题描述】:

几个月前,我想出了一个想法,为我朋友的服务器制作一个功能性的不和谐机器人。我的朋友问我是否可以发出会员超时命令,所以我开始研究。由于 discord.py 将不再更新,这个问题可能没用而且浪费时间,但我真的很想尝试一下。我知道 Stack Overflow 是关于修改代码而不是代码编写服务,但我真的不知道该怎么做。

我的想法是在指定时间内为成员提供“静音”或“超时”角色,并在计时器停止后再次将其删除,但是我应该使用 while 循环导致其余代码不可以再访问了,使用time.sleep 函数也是如此。

我在互联网上找不到任何关于此的信息,所以我唯一的希望是 Stack Overflow。我不希望你在你的答案中编写整个程序,只是我可能正在寻找的地方,这样我就可以自己解决剩下的问题。

提前致谢!

【问题讨论】:

    标签: python discord.py timeout


    【解决方案1】:

    Discord 最近发布了一个非常好的超时功能,它可以解决您创建/删除没有发送消息权限的角色的问题。 Discord.py 可能不会收到更新,但这不会阻止您将自己的请求写入 discord API,或者您也可以使用已经实现此功能的分支之一。

    这是我为使其工作而编写的一个简单的非机器人脚本:

    import pprint
    import requests
    import datetime
    
    BASE = "https://discord.com/api/v9/"
    TOKEN = "BOT TOKEN" 
    
    
    def timeout_user(*, user_id: int, guild_id: int, until: int):
        endpoint = f'guilds/{guild_id}/members/{user_id}'
        headers = {"Authorization": f"Bot {TOKEN}"}
        url = BASE + endpoint
        timeout = (datetime.datetime.utcnow() + datetime.timedelta(minutes=time_in_mins)).isoformat()
        json = {'communication_disabled_until': timeout}
        session = requests.patch(url, json=json, headers=headers)
        if session.status_code in range(200, 299):
            return session.json()
        else: 
            return print("Did not find any\n", session.status_code)
    
    guild_id = 231
    user_id = 123
    time_in_mins = 2
    member = timeout_user(user_id=user_id, guild_id=guild_id,until=time_in_mins)
    pprint.pprint(member)
    
    {'avatar': None,
     'communication_disabled_until': '2021-12-23T09:49:58.965896+00:00',
     'deaf': False,
     'is_pending': False,
     'joined_at': '2021-12-23T09:06:30.927000+00:00',
     'mute': False,
     'nick': None,
     'pending': False,
     'premium_since': None,
     'roles': [],
     'user': {'avatar': 'avatar hash string',
              'discriminator': '1234',
              'id': 'Member ID',
              'public_flags': 128,
              'username': 'Member Name'}}
    

    由于discord.py 是异步的,您可能需要异步代码。 这是一个快速集成

    import aiohttp
    import discord
    import datetime
    import warnings
    from discord.ext import commands
    
    warnings.filterwarnings("ignore", category=DeprecationWarning)
    intents = discord.Intents.all()
    bot = commands.Bot(command_prefix=['$',], intents=intents)
    bot.session = aiohttp.ClientSession()
    
    @bot.event
    async def on_ready():
        print(f"{bot.user.name} is ready to go!")
    
    async def timeout_user(*, user_id: int, guild_id: int, until):
        headers = {"Authorization": f"Bot {bot.http.token}"}
        url = f"https://discord.com/api/v9/guilds/{guild_id}/members/{user_id}"
        timeout = (datetime.datetime.utcnow() + datetime.timedelta(minutes=until)).isoformat()
        json = {'communication_disabled_until': timeout}
        async with bot.session.patch(url, json=json, headers=headers) as session:
            if session.status in range(200, 299):
               return True
            return False
    
    
    @bot.command()
    async def timeout(ctx: commands.Context, member: discord.Member, until: int):
        handshake = await timeout_user(user_id=member.id, guild_id=ctx.guild.id, until=until)
        if handshake:
             return await ctx.send(f"Successfully timed out user for {until} minutes.")
        await ctx.send("Something went wrong")
    
    bot.run("BOT TOKEN")
    

    您可以阅读有关端点here的更多信息

    【讨论】:

    • 好吧,看来它正在工作!最后一个问题,我应该将 JSON 代码保存到不同的文件中,还是可以将其包含在我的 python 代码中?
    • @Itsjul1an 您可能指的是哪个 json 代码?
    • 哦,等等,我附加的 json?您不必保存该 json 代码。这只是 API 在成员超时编辑成功时向您发送的响应。无需担心。
    猜你喜欢
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 2021-09-26
    • 2020-10-24
    相关资源
    最近更新 更多