Discord Py - 取消临时禁止用户的后台任务,在特定服务器中执行取消禁止
Discord Py - Bacckground tasks for unbanning temporarily banned users, perform unban in specific server
我正在尝试制作一个不和谐的审核机器人,但我正在努力设置临时禁令。我有一个后台任务,检查用户在 csv 文件中被临时禁止的日期,如果已经 3 天,则取消禁止用户。我的整个任务都按预期工作,但我这辈子都想不出如何在没有上下文的情况下取消对用户的封禁。
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))
任何能让我朝着正确方向前进的建议都将不胜感激。
我必须创建一个对象才能使其正常工作。
我用过这个
await guild.unban(discord.Object(id=user_id))
而不是
await discord.Guild.unban(user, guild)
我正在尝试制作一个不和谐的审核机器人,但我正在努力设置临时禁令。我有一个后台任务,检查用户在 csv 文件中被临时禁止的日期,如果已经 3 天,则取消禁止用户。我的整个任务都按预期工作,但我这辈子都想不出如何在没有上下文的情况下取消对用户的封禁。
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))
任何能让我朝着正确方向前进的建议都将不胜感激。
我必须创建一个对象才能使其正常工作。
我用过这个
await guild.unban(discord.Object(id=user_id))
而不是
await discord.Guild.unban(user, guild)