development #1

Merged
AllfatherHatt merged 157 commits from development into master 2025-06-14 15:47:26 +02:00
Showing only changes of commit 4639877767 - Show all commits

View File

@ -1,6 +1,6 @@
import discord import discord
from redbot.core import Config, commands # This line imports commands as well from redbot.core import Config, commands
from datetime import datetime, timedelta from datetime import datetime
import pytz import pytz
class TrafficCog(commands.Cog): class TrafficCog(commands.Cog):
@ -17,14 +17,14 @@ class TrafficCog(commands.Cog):
} }
self.config.register_guild(**default_guild) self.config.register_guild(**default_guild)
async def __check_reset(self, guild_id): async def _check_reset(self, guild_id):
async with self.config.guild_from_id(guild_id).all() as guild_data: async with self.config.guild_from_id(guild_id).all() as guild_data:
last_reset = datetime.fromisoformat(guild_data['last_reset']) last_reset = datetime.fromisoformat(guild_data.get('last_reset', datetime.now(pytz.UTC).isoformat()))
if last_reset.date() < datetime.now(pytz.UTC).date(): if last_reset.date() < datetime.now(pytz.UTC).date():
guild_data['daily_stats'] = {"joined": 0, "left": 0, "banned": 0} guild_data['daily_stats'] = {"joined": 0, "left": 0, "banned": 0}
guild_data['last_reset'] = datetime.now(pytz.UTC).isoformat() guild_data['last_reset'] = datetime.now(pytz.UTC).isoformat()
async def __update_stat(self, guild_id, stat_type): async def _update_stat(self, guild_id, stat_type):
async with self.config.guild_from_id(guild_id).all() as guild_data: async with self.config.guild_from_id(guild_id).all() as guild_data:
guild_data['daily_stats'][stat_type] += 1 guild_data['daily_stats'][stat_type] += 1
guild_data['total_stats'][stat_type] += 1 guild_data['total_stats'][stat_type] += 1
@ -44,53 +44,56 @@ class TrafficCog(commands.Cog):
@traffic_commands.command(name='stats') @traffic_commands.command(name='stats')
async def show_stats(self, ctx): async def show_stats(self, ctx):
"""Displays current traffic statistics.""" """Displays current traffic statistics."""
await self.__check_reset(ctx.guild.id) await self._check_reset(ctx.guild.id)
guild_data = await self.config.guild(ctx.guild).all() guild_data = await self.config.guild(ctx.guild).all()
daily_stats = guild_data['daily_stats'] daily_stats = guild_data.get('daily_stats', {"joined": 0, "left": 0, "banned": 0})
total_stats = guild_data['total_stats'] total_stats = guild_data.get('total_stats', {"joined": 0, "left": 0, "banned": 0})
thumbnail_url = guild_data.get('stats_thumbnail_url', 'https://example.com/default-thumbnail.png')
embed = discord.Embed(title="Server Traffic Stats", description="Statistics on server activity", color=0x3399ff) embed = discord.Embed(title="Server Traffic Stats", description="Statistics on server activity", color=0x3399ff)
embed.set_thumbnail(url=guild_data['stats_thumbnail_url']) embed.set_thumbnail(url=thumbnail_url)
embed.add_field(name="Daily Joined", value=daily_stats['joined'], inline=True) embed.add_field(name="Daily Joined", value=daily_stats['joined'], inline=True)
embed.add_field(name="Daily Left", value=daily_stats['left'], inline=True) embed.add_field(name="Daily Left", value=daily_stats['left'], inline=True)
embed.add_field(name="Daily Banned", value=daily_stats['banned'], inline=True) embed.add_field(name="Daily Banned", value=daily_stats['banned'], inline=True)
embed.add_field(name="Total Joined", value=total_stats['joined'], inline=True) embed.add_field(name="Total Joined", value=total_stats['joined'], inline=True)
embed.add_field(name="Total Left", value=total_stats['left'], inline=True) embed.add_field(name="Total Left", value=total_stats['left'], inline=True)
embed.add_field(name="Total Banned", value=total_stats['banned'], inline=True) embed.add_field(name="Total Banned", value=total_stats['banned'], inline=True)
await ctx.send(embed=embed) await ctx.send(embed=embed)
@commands.Cog.listener() @commands.Cog.listener()
async def on_member_join(self, member): async def on_member_join(self, member):
if member.guild.id not in self.config.all_guilds(): await self._check_reset(member.guild.id)
return await self._update_stat(member.guild.id, 'joined')
await self.__check_reset(member.guild.id)
await self.__update_stat(member.guild.id, 'joined')
channel_id = await self.config.guild(member.guild).traffic_channel() channel_id = await self.config.guild(member.guild).traffic_channel()
if channel_id: if not channel_id:
channel = member.guild.get_channel(channel_id) return
if channel: channel = member.guild.get_channel(channel_id) or await member.guild.fetch_channel(channel_id)
await channel.send(f"{member.display_name} has joined the server.") if channel:
await channel.send(f"{member.display_name} has joined the server.")
@commands.Cog.listener() @commands.Cog.listener()
async def on_member_remove(self, member): async def on_member_remove(self, member):
await self.__check_reset(member.guild.id) await self._check_reset(member.guild.id)
await self.__update_stat(member.guild.id, 'left') await self._update_stat(member.guild.id, 'left')
channel_id = await self.config.guild(member.guild).traffic_channel() channel_id = await self.config.guild(member.guild).traffic_channel()
if channel_id: if not channel_id:
channel = member.guild.get_channel(channel_id) return
if channel: channel = member.guild.get_channel(channel_id) or await member.guild.fetch_channel(channel_id)
await channel.send(f"{member.display_name} has left the server.") if channel:
await channel.send(f"{member.display_name} has left the server.")
@commands.Cog.listener() @commands.Cog.listener()
async def on_member_ban(self, guild, member): async def on_member_ban(self, guild, member):
await self.__check_reset(guild.id) await self._check_reset(guild.id)
await self.__update_stat(guild.id, 'banned') await self._update_stat(guild.id, 'banned')
channel_id = await self.config.guild(guild).traffic_channel()
if channel_id:
channel = guild.get_channel(channel_id)
if channel:
await channel.send(f"{member.display_name} has been banned from the server.")
def setup(bot): channel_id = await self.config.guild(guild).traffic_channel()
cog = TrafficCog(bot) if not channel_id:
bot.add_cog(cog) return
channel = guild.get_channel(channel_id) or await guild.fetch_channel(channel_id)
if channel:
await channel.send(f"{member.display_name} has been banned from the server.")