-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.py
More file actions
105 lines (81 loc) · 4.16 KB
/
Copy pathUtility.py
File metadata and controls
105 lines (81 loc) · 4.16 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
import json
import time
import discord
from discord.ext import commands
class Utility(commands.Cog, description="Utility commands related to bot functionality"):
def __init__(self, bot):
self.bot = bot
# prints to the console anything the bot is dm'd
# also stores it in the requests.json file under the dms list
@commands.Cog.listener()
async def on_message(self, message):
if isinstance(message.channel, discord.channel.DMChannel) and message.author != self.bot.user:
print(f"{message.author} sent: {message.content}")
# File where the requests are stored
file_name = 'requests.json'
#Open the file and grab the json data
with open(f"C:/Baka-Bot/{file_name}") as access_json:
file_access = json.load(access_json)
#Get the dms list from the json file
dms = file_access['dms']
# time_gmt stores current gmtime
time_gmt = time.gmtime()
# time_asctime stores the timestamp in a good format
time_asctime = time.asctime(time_gmt)
# Format for the request dict to be stored
dm = {
"author" : f"{message.author}",
"message" : f"{message.content}",
"time_sent" : f"{time_asctime}"
}
# Append the request to the requests list
dms.append(dm)
# Write the requests back into the json file
with open('requests.json', 'w', encoding='utf-8') as access_json:
json.dump(file_access, access_json, ensure_ascii=True, indent=4)
@commands.command(name='request', help='Sends a message to the developer. Please use this to send suggestions for new features, \
report bugs and generally communicate with the developer.')
async def request(self, ctx, *, message):
print(message)
# File where the requests are stored
file_name = 'requests.json'
#Open the file and grab the json data
with open(f"C:/Baka-Bot/{file_name}") as access_json:
file_access = json.load(access_json)
#Get the requests list from the json file
requests = file_access['requests']
# gmt stores current gmtime
time_gmt = time.gmtime()
time_asctime = time.asctime(time_gmt)
# Format for the request dict to be stored
request = {
"author" : f"{ctx.author}",
"message" : f"{message}",
"time_sent" : f"{time_asctime}"
}
# Append the request to the requests list
requests.append(request)
# Write the requests back into the json file
with open('requests.json', 'w', encoding='utf-8') as access_json:
json.dump(file_access, access_json, ensure_ascii=True, indent=4)
await ctx.send(f"Thanks for your request {ctx.author.display_name}!")
# Error handling for the request command
@request.error
async def request_error(self, ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send('A description of your request seems to be missing, try using `!request <description of your request>`')
elif isinstance(error, IOError):
await ctx.send("Something went wrong while opening the request file, please send a dm to either myself or my developer\
and we'll get right on it!")
@commands.command(name='addbot', help='Displays the link used to add me to a server')
async def addbot(self, ctx):
bot_link = "https://discord.com/api/oauth2/authorize?client_id=812441406018420777&permissions=2184707152&scope=bot"
emb = discord.Embed()
emb.set_author(name="Bot URL", icon_url=self.bot.user.avatar_url)
emb.add_field(name=highlight('URL'), value=bot_link)
await ctx.send(embed=emb)
# Helper functions for discord specific text formatting
def highlight(string):
return string.replace(f'{string}',f'` {string} `')
def bold(string):
return string.replace(f'{string}',f'**{string}**')