forked from dpy-blobs/AssBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaut.py
143 lines (117 loc) · 4.55 KB
/
baut.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
import os
import asyncio
import datetime
import pprint
import json
from pathlib import Path
from itertools import cycle
import aiohttp
import discord
from discord.ext import commands
from utils import time, data
class Context(commands.Context):
@property
def session(self):
return self.bot.session
class Bot(commands.Bot):
def __init__(self):
def get_prefix(bot, message):
return message.author.name[0]
super().__init__(command_prefix=get_prefix,
game=discord.Game(name="yes"))
self.last_action = None
self.session = aiohttp.ClientSession(loop=self.loop)
startup_extensions = [x.stem for x in Path('cogs').glob('*.py')]
for extension in startup_extensions:
try:
self.load_extension(f'cogs.{extension}')
except Exception as e:
error = f'{extension}\n {type(e).__name__}: {e}'
print(f'Failed to load extension {error}')
self.loop.create_task(self.cyc())
self.loop.create_task(self.init())
for command in (self._load, self._unload, self._reload):
self.add_command(command)
async def init(self):
await self.wait_until_ready()
self.start_time = datetime.datetime.utcnow()
@property
def uptime(self):
delta = datetime.datetime.utcnow() - self.start_time
return time.human_time(delta.total_seconds())
def _do_cleanup(self):
self.session.close()
super()._do_cleanup()
async def on_ready(self):
self.blob_guild = self.get_guild(328873861481365514)
self.contrib_role = discord.utils.get(self.blob_guild.roles, id=352849291733237771)
await self.http.send_message(352011026738446336, "I'm up!")
print(f'Logged in as {self.user}')
print('-------------')
async def on_message(self, message):
ctx = await self.get_context(message, cls=Context)
if ctx.prefix is not None:
ctx.command = self.all_commands.get(ctx.invoked_with.lower())
await self.invoke(ctx)
async def _run_event(self, coro, event_name, *args, **kwargs):
await super()._run_event(coro, event_name, *args, **kwargs)
if event_name != "on_message":
self.last_action = data.BotAction(coro, event_name, *args, **kwargs)
async def cyc(self):
await self.wait_until_ready()
await asyncio.sleep(3)
guild = self.blob_guild
for member in cycle(self.contrib_role.members):
await guild.me.edit(nick=member.name.upper())
await asyncio.sleep(90)
@commands.command(name='load')
async def _load(self, ctx, *, module: str):
"""Loads a module."""
module = f'cogs.{module}'
try:
self.load_extension(module)
except Exception as e:
await ctx.send('\N{PISTOL}')
await ctx.send(f'{type(e).__name__}: {e}')
else:
await ctx.send('\N{OK HAND SIGN}')
@commands.command(name='reload')
async def _reload(self, ctx, *, module: str):
"""Reloads a module."""
module = f'cogs.{module}'
try:
self.unload_extension(module)
self.load_extension(module)
except Exception as e:
await ctx.send('\N{PISTOL}')
await ctx.send(f'{type(e).__name__}: {e}')
else:
await ctx.send('\N{OK HAND SIGN}')
@commands.command(name='unload')
async def _unload(self, ctx, *, module: str):
"""Unloads a module."""
module = f'cogs.{module}'
try:
self.unload_extension(module)
except Exception as e:
await ctx.send('\N{PISTOL}')
await ctx.send(f'{type(e).__name__}: {e}')
else:
await ctx.send('\N{OK HAND SIGN}')
async def create_gist(self, description, files, pretty=False):
"""
description (str) -- Gist description
files (list[tuple]) -- list of file tuples: (filename, file_contents)
pretty (bool) -- pretty print the contents
"""
if pretty:
file_dict = {f[0]: {"content": pprint.pformat(f[1])} for f in files}
else:
file_dict = {f[0]: {"content": f[1]} for f in files}
payload = {"description": description, "public": True, "files": file_dict}
async with self.session.post("https://api.github.com/gists", data=json.dumps(payload)) as resp:
return (await resp.json())["html_url"]
if __name__ == '__main__':
bot = Bot()
token = os.environ["TOKEN"]
bot.run(token)