Discord.py Timeout server members

We Are Going To Discuss About Discord.py Timeout server members. So lets Start this Python Article.

Discord.py Timeout server members

  1. How to solve Discord.py Timeout server members

    Discord recently released a timeout feature which is really good and takes away your problems of creating/removing a role with no send message perms. Discord.py may not recieve updates but, that does not stop you to write your own requests to discord API, or you can also use one of the forks which have already implemented this.
    Here is one simple non-bot script I wrote to make it work:
    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'}}
    Since discord.py is asynchronous, you might want an asynchronous code.
    Here is one quick integration
    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=[' You can read more about the endpoint here
    ,], 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") You can read more about the endpoint here

  2. Discord.py Timeout server members

    Discord recently released a timeout feature which is really good and takes away your problems of creating/removing a role with no send message perms. Discord.py may not recieve updates but, that does not stop you to write your own requests to discord API, or you can also use one of the forks which have already implemented this.
    Here is one simple non-bot script I wrote to make it work:
    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'}}
    Since discord.py is asynchronous, you might want an asynchronous code.
    Here is one quick integration
    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=[' You can read more about the endpoint here
    ,], 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") You can read more about the endpoint here

Solution 1

Discord recently released a timeout feature which is really good and takes away your problems of creating/removing a role with no send message perms. Discord.py may not recieve updates but, that does not stop you to write your own requests to discord API, or you can also use one of the forks which have already implemented this.

Here is one simple non-bot script I wrote to make it work:

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'}}

Since discord.py is asynchronous, you might want an asynchronous code.
Here is one quick integration

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=['

You can read more about the endpoint here


,], 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")

You can read more about the endpoint here

Original Author Rose Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment