-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathslothbot.py
More file actions
145 lines (119 loc) · 3.67 KB
/
slothbot.py
File metadata and controls
145 lines (119 loc) · 3.67 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
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
import random
import asyncio
import aiohttp
import discord
from discord.ext import commands
from discord.ext.commands import Bot, Context
from lib.util import random_string
import config
# discord intents and client
intents = discord.Intents.default()
# intents.message_content = True
client = discord.Client(intents=intents)
# say command
async def say(saying, channel):
async with channel.typing():
await asyncio.sleep(random.random() * 1)
await channel.send(saying)
return
async def typing(channel):
async with channel.typing():
await asyncio.sleep(random.random() * 1)
return
async def upload(file_handle, file_name, channel):
async with channel.typing():
await asyncio.sleep(random.random() * 2)
await channel.send(file=discord.File(file_handle, file_name))
# ON READY
@client.event
async def on_ready():
# show reboot of bot
channel = client.get_channel(1067446497253265410)
emoji = ["🤣", "🐌", "🐾", "🐨", "💤", "🐼", "🤔", "🐰", "🐻", "🌿","🌼"]
await channel.send(random.choice(emoji))
return
# thread update
@client.event
async def on_thread_update(thread):
print(thread)
return
# thread create
@client.event
async def on_thread_create(prev_thread, next_thread):
print(prev_thread, next_thread)
return
# received a reaction
@client.event
async def on_reaction_add(reaction, user):
channel = client.get_channel(reaction.message.channel.id)
print(channel)
return
# received a plain text query from discord
@client.event
async def on_message(message):
# check if we are allowed to interact with current channel
if message.channel.id not in config.allowed_channel_ids:
return
# if author is the bot, we ignore it from here
if message.author == client.user:
return
# commands
command = message.content.lower().split(" ")[0]
# yann bot
if message.channel.id in config.yann_bot_channel_ids:
command = "!yann"
# check if in commands
if command not in config.commands:
command = "chat"
# return if not allowed general chat in channel
if message.channel.id not in config.chat_channel_ids:
print("returning")
return
else:
command = command.strip("!")
# create a basic document to update as we go
document = {
"document_id": random_string(13),
"message_id": message.id,
"plain": message.content.strip("!"),
"command": command,
"author": message.author.name,
"channel": message.channel.name
}
print(document)
# authenticate
url = config.endpoints_url + "/%s" % command
auth = aiohttp.BasicAuth(
config.basic_auth_username,
config.basic_auth_password
)
try:
await typing(message.channel)
async with aiohttp.ClientSession() as session:
async with session.post(url, auth=auth, json=document) as resp:
assert resp.status == 200
response = await resp.json()
# print(response)
await say(response.get('answer'), message.channel)
# modulate on different response types
if response.get('url_results', []):
for result in response.get('url_results', []):
embed = discord.Embed(
title = result.get('title', ""),
url = result.get('url'),
description = result.get('description'),
color = discord.Color.blue()
)
try:
await typing(message.channel)
await message.channel.send(embed = embed)
except:
pass
if response.get('code'):
await say("```%s```" % response.get('code'), message.channel)
except Exception as ex:
await say("My endpoint handlers for !%s are offline. Standby." % command, message.channel)
print(ex)
return
# entry point
client.run(config.discord_token)