secberg
secberg

Reputation: 31

Discord Py - Bacckground tasks for unbanning temporarily banned users, perform unban in specific server

I am trying to make a discord moderation bot, and I am struggling with having temporary bans setup. I have a background task checking to see what date a user was temporarily banned within a csv file and if its been 3 days then unban the user. I have the whole task working as expected, but cannot for the life of me figure out how to unban the user without context.

import asyncio
import discord
import datetime as dt
import pandas as pd
from discord.ext import commands, tasks


class Events(commands.Cog):
    def __init__(self, client):
        self.client = client  # this allows us to access the client within our cog

    # ------------------- Events and Tasks -------------------
    # Loads bot, and lets us know when its ready
    @commands.Cog.listener()
    async def on_ready(self):
        self.ban_check.start()
        print("Logged in as " + self.client.user.name)
        print(self.client.user.id)
        print("-------")

    # When a known command fails, throws error
    @commands.Cog.listener()
    async def on_command_error(self, ctx, error):
        await ctx.send(ctx.command.name + " didn't work! Give it another try.")
        print(error)

    # Event for monitoring command usage
    @commands.Cog.listener()
    async def on_command(self, ctx):
        print(ctx.command.name + " was invoked.")

    # Event for monitoring successful command usage
    @commands.Cog.listener()
    async def on_command_completion(self, ctx):
        print(ctx.command.name + " was invoked sucessfully.")

    # Task loop to check if its been 3 days since a ban and unbans the user
    @tasks.loop()
    async def ban_check(self):

        for guild in self.client.guilds:

            guild = guild.id

            print(guild)
            await asyncio.sleep(5)
            print("Automatic Task is Running...")
            print("CHECKING BAN LIST")
            df = pd.read_csv('database.csv')
            # member = df["User_ID"]
            bandate = df["Tempban"]

            for date in bandate:
                if str(date) != "nan":
                    print(date)
                    d1 = dt.datetime.strptime(str(date), "%Y-%m-%d")
                    d2 = dt.datetime.strptime(str(dt.date.today()), "%Y-%m-%d")
                    delta = (d2 - d1).days
                    print(delta)

                    if delta >= 3:
                        # guild = self.client.get_guild()
                        print('-------------')
                        user = df.loc[df["Tempban"] == date, "User_ID"]
                        # await self.client.unban(user)
                        # await self.client.guild.unban(user)
                        await discord.Guild.unban(user, guild)
                        print(user)
                    # warnings = int(df.loc[df["User_ID"] == member.id, "Infractions"])
                else:
                    continue


def setup(client):
    client.add_cog(Events(client))

Any suggestions to get me in the right direction would be extremely appreciated.

Upvotes: 2

Views: 454

Answers (1)

secberg
secberg

Reputation: 31

I had to create an object to get this working.

I used this

await guild.unban(discord.Object(id=user_id))

instead of

await discord.Guild.unban(user, guild)

Upvotes: 1

Related Questions