gpt4 book ai didi

python-3.x - 如何使用 discord.py "temporary ban"某人?

转载 作者:行者123 更新时间:2023-12-03 16:45:37 30 4
gpt4 key购买 nike

我正在用 discord.py 制作一个管理 discord 机器人,所以我意识到我需要添加一个命令来临时禁止某人一段时间,这个禁令可以按角色或通过将成员踢出 channel 然后禁止他,但我不知道该怎么做。有人能帮我吗?

最佳答案

经过反复试验,我终于搞定了!给定波纹管是一个 discord.py 机器人,带有临时禁止用户的命令,可用于多个用户

ban_list = []
day_list = []
server_list = []

#This is a background process
async def countdown():
await client.wait_until_ready()
while not client.is_closed:
await asyncio.sleep(1)
day_list[:] = [x - 1 for x in day_list]
for day in day_list:
if day <= 0:
try:
await client.unban(server_list[day_list.index(day)], ban_list[day_list.index(day)])
except:
print('Error! User already unbanned!')
del ban_list[day_list.index(day)]
del server_list[day_list.index(day)]
del day_list[day_list.index(day)]

#Command starts here
@client.command(pass_context = True)
async def ban(ctx,member:discord.Member, days = 1):
if str(ctx.message.author.id) == '<You ID goes here>':
try:
await client.ban(member, delete_message_days=0)
await client.say('User banned for **' + str(days) + ' day(s)**')
ban_list.append(member)
day_list.append(days * 24 * 60 * 60)
server_list.append(ctx.message.server)
except:
await client.say('Error! User not active')
else:
await client.say('You do not have permission to ban users!')

client.loop.create_task(countdown())

enter image description here

I tested this program by banning three users for distinct amounts of time and it worked like a charm. Please note that the time may not be too accurate. The greater the time you choose, the greater the error.

出于某种原因,离线用户无法被 Bot 禁止。

机器人必须全时在线才能工作...如果您重新启动机器人或机器人崩溃,所有列表都会被清除。

关于python-3.x - 如何使用 discord.py "temporary ban"某人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55032363/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com