-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
132 lines (102 loc) · 3.36 KB
/
main.py
File metadata and controls
132 lines (102 loc) · 3.36 KB
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
import discord
from core.models import DiscordMessage
from discord.ext import commands, tasks
from django.conf import settings
from django.db.models import Q
from django.utils import timezone
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def on_ready():
print(f"Bot is ready. Logged in as {bot.user}")
poll_database.start() # Start polling the database
@bot.command()
async def ping(ctx):
await ctx.send("Pong!")
@bot.command()
async def source(ctx):
await ctx.send(
"I'm here: https://github.com/europython/internal-bot",
suppress_embeds=True,
)
@bot.command()
async def wiki(ctx):
await ctx.send(
"Please add it to the wiki: "
"[ep2025-wiki.europython.eu](https://ep2025-wiki.europython.eu)",
suppress_embeds=True,
)
@bot.command()
async def close(ctx):
channel = ctx.channel
author = ctx.message.author
# Check if it's a public or private post (thread)
if channel.type in (
discord.ChannelType.public_thread,
discord.ChannelType.private_thread,
):
parent = channel.parent
# Check if the post (thread) was sent in a forum,
# so we can add a tag
if parent.type == discord.ChannelType.forum:
# Get tag from forum
tag = None
for _tag in parent.available_tags:
if _tag.name.lower() == "done":
tag = _tag
break
if tag is not None:
await channel.add_tags(tag)
# Remove command message
await ctx.message.delete()
# Send notification to the thread
await channel.send(
f"# This was marked as done by {author.mention}", suppress_embeds=True
)
# We need to archive after adding tags in case it was a forum.
await channel.edit(archived=True)
else:
# Remove command message
await ctx.message.delete()
await channel.send(
"The !close command is intended to be used inside a thread/post",
suppress_embeds=True,
delete_after=5,
)
@bot.command()
async def version(ctx):
app_version = settings.APP_VERSION
await ctx.send(f"Version: {app_version}")
@bot.command()
async def qlen(ctx):
qs = get_messages()
# qs = DiscordMessage.objects.afilter(sent_at__isnull=True)
cnt = await qs.acount()
await ctx.send(f"In the queue there are: {cnt} messages")
def get_messages():
messages = DiscordMessage.objects.filter(
Q(send_after__isnull=True) | Q(send_after__lte=timezone.now()),
sent_at__isnull=True,
)
return messages
@tasks.loop(seconds=60) # Seconds
async def poll_database():
"""Check for unsent messages and send them."""
messages = get_messages()
print("Polling database.... ", timezone.now())
async for message in messages:
channel = bot.get_channel(int(message.channel_id))
if channel:
await channel.send(
message.content,
suppress_embeds=True,
)
message.sent_at = timezone.now()
await message.asave()
else:
print("Channel does not exist!")
def run_bot():
bot_token = settings.DISCORD_BOT_TOKEN
bot.run(bot_token)