-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
83 lines (63 loc) · 2.6 KB
/
main.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
#!/bin/env python
import os
import discord
from discord.ext import commands
# setup of logging and env-vars
# logging must be initialized before environment, to enable logging in environment
from .log_setup import logger
from .environment import PREFIX, TOKEN
"""
This bot is based on a template by nonchris
https://github.com/nonchris/discord-bot
"""
intents = discord.Intents.all()
# inspired by https://github.com/Rapptz/RoboDanny
# This function will be evaluated for each message
# you can define specific behaviours for different messages or guilds, like custom prefixes for a guild etc...
def _prefix_callable(_bot: commands.Bot, msg: discord.Message):
user_id = _bot.user.id
# way discord expresses mentions
# mobile and desktop have a different syntax how mentions are sent, so we handle both
prefixes = [f'<@!{user_id}> ', f'<@{user_id}> ']
if msg.guild is None: # we're in DMs, using default prefix
prefixes.append(PREFIX)
return prefixes
# TODO: This would be the place to add guild specific custom prefixes
# you've got the current message hence the guild-id which is perfect to store and load prefixes for a guild
# just append them to base and only append the default prefix if there is no custom prefix for that guild
prefixes.append(PREFIX)
return prefixes
bot = commands.Bot(command_prefix=_prefix_callable, intents=intents)
# login message
@bot.event
async def on_ready():
"""!
function called when the bot is ready. Emits the '[Bot] has connected' message
"""
print()
member_count = 0
guild_string = ""
for g in bot.guilds:
guild_string += f"{g.name} - {g.id} - Members: {g.member_count}\n"
member_count += g.member_count
logger.info(f"Bot '{bot.user.name}' has connected, active on {len(bot.guilds)} guilds:\n{guild_string}")
await bot.change_presence(
activity=discord.Activity(type=discord.ActivityType.watching, name=f"{PREFIX}help"))
# LOADING Extensions
# this is done in on_ready() so that cogs can fetch data from discord when they're loaded
bot.remove_command('help') # unload default help message
# TODO: Register your extensions here
initial_extensions = [
'.cogs.misc',
'.cogs.help'
]
for extension in initial_extensions:
bot.load_extension(extension, package=__package__)
def start_bot(token=None):
""" Start the bot, takes token, uses token from env if none is given """
if token is not None:
bot.run(token)
if TOKEN is not None:
bot.run(TOKEN)
else:
logger.error("No token was given! - Exiting")