-
Notifications
You must be signed in to change notification settings - Fork 0
/
greetings.py
107 lines (89 loc) · 4.56 KB
/
greetings.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
"""
Manages `welcome` info-channel and automatic role giving
Also some 'fun' commands
"""
from datetime import datetime, timedelta
from typing import List
import discord
from discord.ext import commands, tasks
class QueueItem:
def __init__(self, user: discord.Member, roles: List[discord.Role]):
self.user = user
self.roles = roles
class Greetings(commands.Cog):
queue: List[QueueItem]
def __init__(self, bot: commands.Bot):
print('Loading Greetings module...', end='')
self.bot = bot
self.guild_config_cog = bot.get_cog('GuildConfigCog')
self.queue = []
self.allowed_mentions = discord.AllowedMentions(everyone=False, users=False, roles=False)
# Disabling pylint error because it analyses code improperly, which results in error being reported.
self.queue_checker.start() # pylint: disable=no-member
print(' Done')
@commands.Cog.listener()
async def on_member_join(self, member: discord.Member):
guild_config = await self.guild_config_cog.get_guild(member.guild)
welcome_channel = await guild_config.get_welcome_channel()
default_roles = await guild_config.get_default_roles()
if welcome_channel:
await welcome_channel.send('Welcome, {0.mention}'.format(member))
if default_roles:
if member.guild.verification_level != discord.VerificationLevel.none:
print("Putting user {} on queue".format(member.id)) # type: ignore
self.queue.append(QueueItem(member, default_roles))
else:
await member.add_roles(*default_roles, reason='New member join.')
@commands.Cog.listener()
async def on_member_remove(self, member: discord.Member):
guild_config = await self.guild_config_cog.get_guild(member.guild)
welcome_channel = await guild_config.get_welcome_channel()
if welcome_channel:
await welcome_channel.send('Goodbye, {0} (ID:{1})'.format(str(member), member.id), allowed_mentions=self.allowed_mentions) # type: ignore
@tasks.loop(seconds=10)
async def queue_checker(self):
if len(self.queue) > 0:
print('Checking queue. {} items'.format(len(self.queue)))
queue_to_remove = []
for queue_item in self.queue:
if self.check_queued(queue_item.user):
print('User {} is now valid for automatic role giving'.format(queue_item.user.id)) # type: ignore
await queue_item.user.add_roles(*queue_item.roles, reason='New member join')
queue_to_remove.append(queue_item)
for queue_item in queue_to_remove:
print('Removing user {} from queue'.format(queue_item.user.id))
self.queue.remove(queue_item)
def check_queued(self, member: discord.Member):
if member.joined_at is None:
for item in self.queue:
if item.user == member:
self.queue.remove(item)
return False
guild_vl = member.guild.verification_level
mediumlevel = True
highlevel = True
if guild_vl >= discord.VerificationLevel.medium:
mediumlevel = datetime.now() - member.created_at > timedelta(minutes=5) # type: ignore
if guild_vl >= discord.VerificationLevel.high:
highlevel = datetime.now() - member.joined_at > timedelta(minutes=10)
return mediumlevel and highlevel
@commands.command(description='\"Hello\" in English', brief='\"Hello\" in English', help='Returns \"Hello\" in English')
async def hello(self, ctx: commands.Context):
await ctx.send('Hello!')
@commands.command(aliases=['privet'], description='\"Hello\" in Russian', brief='\"Hello\" in Russian', help='Returns \"Hello\" in Russian')
async def hello_russian(self, ctx: commands.Context):
await ctx.send('Приветствую!')
@commands.command(aliases=['gutentag'], description='\"Hello\" in German', brief='\"Hello\" in German', help='Returns \"Hello\" in German')
async def hello_german(self, ctx: commands.Context):
await ctx.send('Guten tag')
@commands.command(hidden=True, help='tag')
async def guten(self, ctx: commands.Context):
await ctx.send('tag')
@commands.command(hidden=True, aliases=['youok?'])
async def is_alive_eng(self, ctx: commands.Context):
await ctx.send('yes')
@commands.command(hidden=True, aliases=['живой?'])
async def is_alive_rus(self, ctx: commands.Context):
await ctx.send('Да')
def setup(bot):
bot.add_cog(Greetings(bot))