Fixed bug where role members without timeout werent detected

This commit is contained in:
iAmInAction 2024-04-02 19:48:51 +02:00 committed by GitHub
parent 02a78c213f
commit 8064ea0626
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -13,7 +13,7 @@
# Import libraries (you need to install the discord library with "pip3 install discord") # Import libraries (you need to install the discord library with "pip3 install discord")
import discord import discord
import asyncio import asyncio
from datetime import datetime, timedelta from datetime import datetime, timedelta, timezone
from discord.ext import tasks from discord.ext import tasks
# CONFIGURATION SECTION # CONFIGURATION SECTION
@ -52,22 +52,29 @@ async def on_ready():
@tasks.loop(minutes=30) @tasks.loop(minutes=30)
async def check_spam_role_and_timeout(): async def check_spam_role_and_timeout():
global guild, channel, role, user_messages global guild, channel, role, user_messages
user_messages.clear() # Clear user_messages dictionary
if role is None: if role is None:
await channel.send(f"Role '{spam_role_name}' not found.") await channel.send(f"Role '{spam_role_name}' not found.")
return return
# Get all members who have the role but are not timed out # Get all members who have the role but are not timed out
members_with_role = [member for member in guild.members if role in member.roles and not any(role.name == "Certified Spam" for role in member.roles)] untimed_members = []
if members_with_role: for member in guild.members:
timeout_message = "Users with the 'Certified Spam' role:" if role in member.roles:
for member in members_with_role: if not any(role.name == "Certified Spam" for role in member.roles):
timeout_message += f"\n{member.display_name}" continue # Skip if already Certified Spam
# Apply timeout for 1 day if not member.is_timed_out():
await member.timeout(datetime.utcnow() + timedelta(days=1), reason="Certified Spam") untimed_members.append(member)
await channel.send(timeout_message) # Apply timeout for 1 day
else: await member.timeout(datetime.now(timezone.utc) + timedelta(days=1), reason="Certified Spam")
if not untimed_members:
print("No users with the 'Certified Spam' role found who are not timed out.") print("No users with the 'Certified Spam' role found who are not timed out.")
else:
timeout_message = "New 'Certified Spam' role members:\n"
for member in untimed_members:
timeout_message += f"{member.display_name}\n"
await channel.send(timeout_message)
@client.event @client.event
async def on_message(message): async def on_message(message):