-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
94 lines (73 loc) · 3.69 KB
/
bot.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
import sys
import discord
from discord import option
import yaml
import logging
import config
from mybot import MyBot
logging.basicConfig(level=logging.INFO)
# Set to remember if the bot is already running, since on_ready may be called
# more than once on reconnects
this = sys.modules[__name__]
this.running = False
yaml_filename = "config_local.yaml"
cfg = yaml.safe_load(open(yaml_filename))
logging.info(f"Config: {config.censor_config(cfg)}")
if cfg.get('DEBUG'):
logging.warning("Debug mode active - do not run in production!")
# Initialize the client
logging.info("Starting up...")
bot = discord.Bot(intents=discord.Intents(message_content=True, guild_messages=True, guilds=True, messages=True, members=True))
mybot = None
# Define event handlers for the client
# on_ready may be called multiple times in the event of a reconnect,
# hence the running flag
@bot.event
async def on_ready():
global mybot
if this.running: return
else: this.running = True
print("Client started up.", flush=True)
mybot = MyBot(cfg["guild_id"], cfg["bot_channel_id"], cfg["midnight_channel_id"], cfg["lukija_role_id"], cfg["osallistuja_role_id"], cfg["db_name"], cfg["db_user"], cfg["db_password"], cfg["admin_user_id"], cfg["activity_ignore_channel_ids"], bot)
mybot.startup()
@bot.event
async def on_message(message: discord.Message):
if cfg.get('DEBUG'):
logging.info(f"on_message {message.content}")
else:
logging.info(f"on_message {message.id}")
if message.guild is None:
return # DM?
await mybot.process_message(message)
@bot.event
async def on_member_join(member: discord.Member):
await mybot.on_member_join(member)
@bot.slash_command(guild_ids=[cfg["guild_id"]], name="midnight-winners", description="Midnight winners")
async def midnight_winners(ctx):
await mybot.midnight_winners_command(ctx)
#@bot.slash_command(guild_ids=guild_ids, name="threads", description="List of threads")
#async def threads(ctx):
#await instances[ctx.guild_id].list_threads_command(ctx)
# pass
@bot.slash_command(name="viestilaskuri", description="Viimeisen 3kk:n viestimäärä (vastaus näkyy vain sinulle)")
async def message_count(ctx):
await mybot.message_count_command(ctx)
pass
@bot.slash_command(guild_ids=[cfg["guild_id"]], name="toisen-viestilaskuri", description="Toisen käyttäjän viimeisen 3kk:n viestimäärä (vain moderaattoreille, vastaus näkyy vain sinulle).")
async def message_count_other(ctx, käyttäjä: discord.Option(discord.SlashCommandOptionType.user)): # "käyttäjä" is in Finnish because it shows in the slash command help"
await mybot.message_count_other_command(ctx, käyttäjä)
pass
@bot.slash_command(name="post-copy", description="Tekee lähdeviestistä kopion, jonka botti julkaisee annetulla kanavalla.")
@option("kanava", discord.SlashCommandOptionType.channel, description="Kanava, jonne viesti kopioidaan")
@option("lähdeviesti", str, description="Kopioitava viesti, täysi URL tai kanavan sisäisesti Message ID")
async def post_copy(ctx, kanava, lähdeviesti): # "kanava" and "lähdeviesti" are in Finnish because they're visible in command context help
await mybot.post_copy(ctx, kanava, lähdeviesti)
pass
@bot.slash_command(name="post-edit",
description="Muokkaa kohdeviestiä kopioimalla sen tilalle lähdeviestin sisältö. Kohdeviestin oltava botin oma.")
@option("kohdeviesti", str, description="Muokattava viesti, täysi URL tai kanavan sisäisesti Message ID")
@option("lähdeviesti", str, description="Kopioitava viesti, täysi URL tai kanavan sisäisesti Message ID")
async def post_edit(ctx, kohdeviesti, lähdeviesti):
await mybot.post_edit(ctx, kohdeviesti, lähdeviesti)
pass
bot.run(cfg["token"])