/
bot.py
212 lines (160 loc) · 5.89 KB
/
bot.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
201
202
203
204
205
206
207
208
209
210
211
212
# bot.py
import aiohttp
import eliza
import hupper
import json
from dotenv import load_dotenv
from discord.ext import commands
from discord.http import HTTPClient, Route
from discord.message import Message
from discord.embeds import Embed
from discord.state import ConnectionState
import discord
import os
import sys
import logging
load_dotenv()
LOG_LEVEL = os.getenv('LOG_LEVEL')
if LOG_LEVEL and hasattr(logging, LOG_LEVEL):
log_level = getattr(logging, LOG_LEVEL)
else:
log_level = logging.INFO
print('LOG_LEVEL is %s' % LOG_LEVEL)
print('log_level is %s' % log_level)
logging.basicConfig(level=log_level)
log = logging.getLogger('bot.py')
log.info('info level works')
TOKEN = os.getenv('DISCORD_TOKEN')
FOOTBALL_DATA_TOKEN = os.getenv('FOOTBALL_DATA_TOKEN')
intents = discord.Intents.default()
intents.members = True
class Interaction:
def __init__(self, channel, data, state):
self.channel = channel
self.data = data
self.state = state
class MyConnectionState(ConnectionState):
def parse_interaction_create(self, data):
channel, _ = self._get_guild_channel(data)
i = Interaction(channel=channel, data=data, state=self)
self.dispatch('interaction', i)
class MyBot(commands.Bot):
def _get_state(self, **options):
return MyConnectionState(dispatch=self.dispatch, handlers=self._handlers,
hooks=self._hooks, syncer=self._syncer, http=self.http, loop=self.loop, **options)
bot = MyBot(command_prefix='/', description='unsmart bot', intents=intents)
# bot = commands.Bot(command_prefix='/', description='unsmart bot', intents=intents)
# client = discord.Client()
client = bot
therapists = {}
@bot.command()
async def add(ctx, left: int, right: int):
"""Adds two numbers together."""
await ctx.send(left + right)
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
def get_command_option(command_data, name):
opts = [opt for opt in command_data['options'] if opt['name'] == name]
if opts:
return opts[0]['value']
@client.event
async def on_interaction(i: Interaction):
log.info('interaction channel=%s', i.channel)
# log.info('interaction data is %s', i.data)
iid = i.data['id']
itoken = i.data['token']
msg = 'What?'
embeds = []
greet_command = '%s-greet' % client.user.name.lower()
todays_matches = '%s-foci-ma' % client.user.name.lower()
command_data = i.data['data']
if command_data['name'] == greet_command:
greetee = get_command_option(command_data, 'name')
age = get_command_option(command_data, 'age')
if age is not None:
greetee += ' (%s)' % age
msg = 'Hello, %s!' % greetee
elif command_data['name'] == todays_matches:
embed = await get_todays_matches()
embeds.append(embed)
payload = {
'type': 4,
'data': {
'content': msg,
'embeds': embeds
}
}
http_client: HTTPClient = i.state.http
route = Route('POST', '/interactions/%s/%s/callback' % (iid, itoken))
await http_client.request(route, json=payload)
async def get_todays_matches():
resp = await do_get_todays_matches()
count = resp['count']
return {
'color': 0x0099ff,
'title': 'Football',
'type': 'rich',
'fields': [
{
'name': TODAYS_MATCHES,
'value': '%s' % count,
},
]}
async def do_get_todays_matches():
async with aiohttp.ClientSession() as session:
url = 'https://api.football-data.org/v2/matches'
headers = {"X-Auth-Token": FOOTBALL_DATA_TOKEN}
request_context = session.request(
method='GET', url=url, headers=headers)
async with request_context as response:
response_text = await response.text()
return json.loads(response_text)
TODAYS_MATCHES = "Today's matches:"
async def send_matches(message: discord.message.Message):
resp = await do_get_todays_matches()
for m in resp['matches']:
e = Embed()
e.title = m['competition']['name']
e.description = m['competition']['area']['name']
e.set_image(url=m['competition']['area']['ensignUrl'])
e.add_field(name='Time', value=m['utcDate'],inline=False)
e.add_field(name='Status', value=m['status'],inline=False)
e.add_field(name=m['homeTeam']['name'], value=m['score']
['fullTime']['homeTeam'], inline=True)
e.add_field(name=m['awayTeam']['name'], value=m['score']
['fullTime']['awayTeam'], inline=True)
await message.reply(embed=e)
@ client.event
async def on_message(message: discord.message.Message):
if message.author == client.user:
# check our own messages:
if len(message.embeds) == 1:
e: Embed = message.embeds[0]
if e.title == 'Football':
f0 = e.fields[0]
if f0.name == TODAYS_MATCHES:
await send_matches(message)
return
log.info('message received: %s', message.content)
log.info('message channel=%s', message.channel)
log.info('message author is %s', message.author.display_name)
if message.content.lower() == 'ping':
await message.channel.send('Pong!')
other = message.author.display_name
therapist = therapists.get(other)
if therapist:
await message.reply(therapist.respond(message.content))
if message.content == 'quit':
del therapists[other]
elif message.content == 'Eliza?':
therapist = eliza.eliza()
therapists[other] = therapist
await message.reply("Hello. How are you feeling today?")
def main(args=sys.argv[1:]):
if '--reload' in args:
# start_reloader will only return in a monitored subprocess
hupper.start_reloader('bot.main')
client.run(TOKEN)
if __name__ == '__main__':
main()