-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
200 lines (179 loc) · 6.55 KB
/
app.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import discord
from discord.ext import commands
import logging
import yaml
import random
import re
# the config YAML is made globally available
global config
with open('./config.yaml', 'r') as file:
config = yaml.safe_load(file)
# Setting intents
intents = discord.Intents.default()
intents.message_content = True
intents.typing = False
intents.presences = False
intents.members = True
command_prefix = '/'
help_command = commands.DefaultHelpCommand(
no_category = 'Commands'
)
description = ''
activity = discord.Activity(name="over this server", type=discord.ActivityType.watching)
status = discord.Status.online
bot = commands.Bot(
command_prefix = commands.when_mentioned_or(command_prefix),
intents = intents,
activity = activity,
stauts = status,
case_insensitive = True,
help_command = help_command,
description = description,
owner_id = ['456175197415014403', '287663053707673600'], # caelestia-42bit (https://github.com/caelestia-42bit), ShaeCalmine
shard_count = config['app']['discord_api']['shard_count'],
shard_id = config['app']['discord_api']['shard_id']
)
# Set up the logger (logging and internal)
handler = None
level = None
if config['app']['logging']['activate']:
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
level = str(config['app']['logging']['level'])
# Alternative/backup logging (internal, using python3-logging)
logging.basicConfig(level=level, handlers=[handler])
@bot.event
async def on_ready():
print(f'Connected to Discord as {bot.user.name} with ID: {bot.user.id}')
print(f'Running API version {discord.__version__}')
print(f'Logging: {config["app"]["logging"]["activate"]}, Level: {level}')
logging.debug(config)
return
# ping the bot
@bot.command(name='ping')
async def ping(ctx):
await ctx.send(f'Pong!\n\nI am a bot, logged in as "{bot.user.name}" with ID: {bot.user.id}.\nI am using the Discord API version {discord.__version__}.\nAnd how are you doing, human?')
logging.info('PING!')
return
@bot.command(name='cookie')
async def cookie(ctx, amount=1):
cookies = []
for _ in range(amount):
cookies.append('🍪')
await ctx.send(str(" ".join(cookies)))
@bot.command(name='parse')
async def parse(ctx, *args):
await ctx.send(args)
# say hi
@bot.command(name='hi')
async def hi(ctx):
await ctx.send(f'Hello, human!')
# test command, just for me
@bot.command(name='test')
async def test(ctx, *args):
arguments = ', '.join(args)
await ctx.send(f'{len(args)} arguments: {arguments}')
# roll custom ammount of multi-sided dice
# TODO Check for the right amount of arguments passed! (errors out)
@bot.command(name='roll')
async def roll(ctx, amount: str, dice: str):
valid_dice = config['app']['dice_roller']['valid_dice']
if not dice.startswith('d') or not dice[1:].isdigit() or not any(str(substring) in dice[1:] for substring in valid_dice):
await ctx.send(f'"{dice}" is not a valid dice. A diece begins with "d" followed by one of the following numbers of sides: {", ".join(map(str, valid_dice))}.')
return
if not amount.isdigit():
await ctx.send(f'{amount} is not a valid dice amount... obviously.')
return
else: amount = int(amount)
if amount > 500 or amount < 1:
await ctx.send(f'{amount} is too high or too low.')
return
rolls = [random.randint(1, int(dice[1:])) for _ in range(amount)]
await ctx.send(f'{", ".join(map(str, rolls))}')
return
"""
# change bot activity
@bot.command(name='chact')
async def chact(ctx, activity):
try:
bot.activity = discord.Activity(name=activity, type=discord.ActivityType.custom)
except:
await ctx.send(f'Failed to change the {bot.user.name}\'s activity.')
logging.error(f'Failed to process chactivity request by {ctx.user.name}')
logging.info(f'{ctx.user.name} changed {bot.user.name}\'s activity to: "{activity}"')
return
"""
"""
# change bot status
@bot.command(name='chst')
async def chst(ctx, status):
try:
match status:
case "dnd":
bot.status = discord.Status.dnd
case "online":
bot.status = discord.Status.online
case "offline":
bot.status = discord.Status.offline
case "idle":
bot.status = discord.Status.idle
except:
await ctx.send(f'There was an issue with setting {bot.user.name}\'s status to "{status}"')
return
"""
# Shutdown bot (owner)
@bot.command(name='shutdown')
@commands.is_owner()
async def shutdown(ctx):
await ctx.send(f'{ctx.user.name} issued the bot.close() command. Shutting down!')
logging.info(f'{ctx.user.name} issued the bot.close() command.')
logging.shutdown
await bot.close()
'''
# Custom help command, overriding the default help of discord.ext
class MyHelpCommand(commands.DefaultHelpCommand):
async def send_bot_help(self, mapping):
embed = discord.Embed(
title=f'{bot.user.name}',
description="For meta information, refer to the documentation on https://github.com/caelestia-42bit/The_Phantom",
color=discord.Color.brand_red(),
url=None,
)
embed.add_field(
name=f'{command_prefix}help',
value='The help command calls up this page, you are reading right now. Duh.',
inline=True
)
embed.add_field(
name=f'{command_prefix}ping',
value='Poke the bot...',
inline=True
)
embed.add_field(
name=f'{command_prefix}roll <dice amount> <dice type>',
value=f'Roll a dice. A diece begins with "d" followed by one of the following numbers of sides: {", ".join(map(str, config["app"]["dice_roller"]["valid_dice"]))}',
inline=True
)
embed.add_field(
name=f'{command_prefix}chact <activity>',
value='Changes the bots activity callout.',
inline=True
)
embed.add_field(
name=f'{command_prefix}chst <status>',
value='Changes the bots status. Select either online, offline, idle, or dnd.',
inline=True
)
embed.set_author(
name=f'Created by The Phantasm Bot Projects',
icon_url=None
)
await self.get_destination().send(embed=embed)
bot.help_command = MyHelpCommand()
'''
if __name__ == "__main__":
print('running...')
bot.run(
config['app']['discord_api']['token'],
log_handler=handler,
log_level=level
)