This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsayo.py
136 lines (101 loc) · 4.3 KB
/
sayo.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
# -*- coding: utf-8 -*-
# Copyright (C) 2019 Moonmoon
import discord
from config import bot_config
sayo = None
class Sayo(object):
client = discord.Client()
def __init__(self):
self.end_bot = False
self.starts_with = bot_config['discord'].get('command', '$')
self.commands = {
self.starts_with + "count": self.count_votes,
self.starts_with + "die": self.die
}
### Client Events
@client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == Sayo.client.user or message.author.bot:
return
if message.content.startswith(sayo.starts_with):
if message.author.id not in bot_config['discord'].get('user_ids', list()):
await sayo.send_message(message.channel, "Sorry you're not authorized to use this bot.")
return
command = message.content
params = list()
if ' ' in message.content:
command, params = (message.content.split()[0], message.content.split()[1:])
command = command.lower()
if command in sayo.commands:
await sayo.commands[command](command, params, message)
@client.event
async def on_ready():
print('Logged in as')
print(Sayo.client.user.name)
print(Sayo.client.user.id)
print('------')
### RUN IT!
def run(self):
TOKEN = bot_config.get('bot_token')
if not TOKEN:
print("The provided token is either invalid or unspecified: ", TOKEN)
return
self.client.run(TOKEN)
### Helpers
async def send_message(self, channel, message):
await channel.send(message)
async def send_embed_message(self, channel, message="", title="", colour=discord.Colour.dark_green(), fields=[], image=None):
embed = discord.Embed(
title=title,
type="rich",
colour=colour
)
for field in fields:
embed.add_field(name=field['name'],
value=field['value'],
inline=field['inline'])
if image:
embed.set_image(url=image)
await channel.send(content=message, embed=embed)
### Commands
async def count_votes(self, command, params, message):
voting_channel_id = bot_config.get("discord").get('voting_channel_id', None)
results_channel_id = bot_config.get("discord").get('results_channel_id', None)
if not voting_channel_id:
await sayo.send_message(message.channel, "Voting channel ID was not properly configured. Aborting command.")
return
if not results_channel_id:
await sayo.send_message(message.channel, "Results channel ID was not properly configured. Aborting command.")
return
voting_channel = sayo.client.get_channel(voting_channel_id)
messages = await voting_channel.history(limit=100).flatten()
messages = sorted(messages, key=lambda msg: sum([react.count for react in msg.reactions]), reverse=True)
if len(messages) > 8: messages = messages[:8]
results_channel = sayo.client.get_channel(results_channel_id)
async with results_channel.typing():
for msg in messages:
if not msg.clean_content.startswith("https"):
# messages that are not links are not submissions, so skip
continue
fields = [
{
'name': "Message Link",
'value': msg.jump_url,
'inline': False
},
{
'name': 'Vote Count',
'value': sum([react.count for react in msg.reactions]),
'inline': False
}
]
await sayo.send_embed_message(results_channel, fields=fields, image=msg.clean_content)
async def die(self, command, params, message):
msg = 'Shutting down...'
sayo.end_bot = True
await sayo.send_message(message.channel, msg)
await sayo.client.close()
if __name__ == '__main__':
sayo = Sayo()
sayo.run()