【发布时间】:2021-03-30 15:19:10
【问题描述】:
我正在尝试制作一个不和谐的审核机器人,但我正在努力设置临时禁令。我有一个后台任务检查,以查看用户在 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))
任何能让我走上正确方向的建议都将不胜感激。
【问题讨论】:
-
参见 this 作为获取/获取对象并相应更改代码的示例
-
@EthanM-H 我查看了您提供的内容,并对我的代码进行了一些重大更改,但我似乎无法让它在指定的服务器中执行解禁操作。我相信我有正确的语法,但我不断得到一个对象对于我尝试的任何事情都没有属性'unban'。还有其他建议吗?
-
您需要在
Guild的实例上调用 unban,而不是对类的引用。await ctx.guild.unban(user)事物类型
标签: python discord scheduled-tasks discord.py