forked from elijabesu/SauriCogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserlog.py
155 lines (132 loc) · 5.61 KB
/
userlog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from datetime import datetime, timezone
import discord
import typing
from redbot.core import checks, commands, Config
from redbot.core.bot import Red
class UserLog(commands.Cog):
"""
Log when users join/leave into a specified channel.
"""
__version__ = "1.1.0"
def __init__(self, bot: Red):
self.bot = bot
self.config = Config.get_conf(
self, identifier=56546565165465456, force_registration=True
)
self.config.register_guild(channel=None, join=True, leave=True)
async def red_delete_data_for_user(self, *, requester, user_id):
# nothing to delete
return
def format_help_for_context(self, ctx: commands.Context) -> str:
context = super().format_help_for_context(ctx)
return f"{context}\n\nVersion: {self.__version__}"
@commands.group(autohelp=True, aliases=["userlog"])
@commands.guild_only()
@checks.admin()
async def userlogset(self, ctx: commands.Context):
"""Various User Log settings."""
@userlogset.command(name="channel")
async def user_channel_log(
self, ctx: commands.Context, channel: typing.Optional[discord.TextChannel]
):
"""Set the channel for logs.
If the channel is not provided, logging will be disabled."""
if channel:
await self.config.guild(ctx.guild).channel.set(channel.id)
else:
await self.config.guild(ctx.guild).channel.clear()
await ctx.tick()
@userlogset.command(name="join")
async def user_join_log(self, ctx: commands.Context, on_off: typing.Optional[bool]):
"""Toggle logging when users join the current server.
If `on_off` is not provided, the state will be flipped."""
target_state = on_off or not (await self.config.guild(ctx.guild).join())
await self.config.guild(ctx.guild).join.set(target_state)
if target_state:
await ctx.send("Logging users joining is now enabled.")
else:
await ctx.send("Logging users joining is now disabled.")
@userlogset.command(name="leave")
async def user_leave_log(
self, ctx: commands.Context, on_off: typing.Optional[bool]
):
"""Toggle logging when users leave the current server.
If `on_off` is not provided, the state will be flipped."""
target_state = on_off or not (await self.config.guild(ctx.guild).leave())
await self.config.guild(ctx.guild).leave.set(target_state)
if target_state:
await ctx.send("Logging users leaving is now enabled.")
else:
await ctx.send("Logging users leaving is now disabled.")
@userlogset.command(name="settings")
async def user_settings(self, ctx: commands.Context):
"""See current settings."""
data = await self.config.guild(ctx.guild).all()
channel = ctx.guild.get_channel(await self.config.guild(ctx.guild).channel())
channel = "None" if not channel else channel.mention
embed = discord.Embed(
colour=await ctx.embed_colour(), timestamp=datetime.now()
)
embed.set_author(name=ctx.guild.name, icon_url=ctx.guild.icon)
embed.title = "**__User Log settings:__**"
embed.set_footer(text="*required to function properly")
embed.add_field(name="Channel*:", value=channel)
embed.add_field(name="Join:", value=str(data["join"]))
embed.add_field(name="Leave:", value=str(data["leave"]))
await ctx.send(embed=embed)
@commands.Cog.listener()
async def on_member_join(self, member):
join = await self.config.guild(member.guild).join()
if not join:
return
channel = member.guild.get_channel(
await self.config.guild(member.guild).channel()
)
if not channel:
return
time = datetime.now(timezone.utc)
users = len(member.guild.members)
since_created = (time - member.created_at).days
user_created = member.created_at.strftime("%Y-%m-%d, %H:%M")
created_on = f"{user_created} ({since_created} days ago)"
embed = discord.Embed(
description=f"{member.mention} ({member.name}#{member.discriminator})",
colour=discord.Colour.green(),
timestamp=member.joined_at,
)
embed.add_field(name="Total Users:", value=str(users))
embed.add_field(name="Account created on:", value=created_on)
embed.set_footer(text=f"User ID: {member.id}")
embed.set_author(
name=f"{member.name} has joined the guild",
url=member.avatar,
icon_url=member.avatar,
)
embed.set_thumbnail(url=member.avatar)
await channel.send(embed=embed)
@commands.Cog.listener()
async def on_member_remove(self, member):
leave = await self.config.guild(member.guild).leave()
if not leave:
return
channel = member.guild.get_channel(
await self.config.guild(member.guild).channel()
)
if not channel:
return
time = datetime.now()
users = len(member.guild.members)
embed = discord.Embed(
description=f"{member.mention} ({member.name}#{member.discriminator})",
colour=discord.Colour.red(),
timestamp=time,
)
embed.add_field(name="Total Users:", value=str(users))
embed.set_footer(text=f"User ID: {member.id}")
embed.set_author(
name=f"{member.name} has left the guild",
url=member.avatar,
icon_url=member.avatar,
)
embed.set_thumbnail(url=member.avatar)
await channel.send(embed=embed)