-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord_main.py
218 lines (184 loc) · 8.98 KB
/
discord_main.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
213
214
215
216
217
218
# This is the main program to activate the discord bot, to change the bot with your own bot, simply change the discord bot
# token in the .env file. For more details, please ask for chatGPT and visit online tutorials.
# For test run or run this in a docker image, please take a look at the docker-compose.yml or docker-compose-swarm.yml
import discord
from discord.ext import commands, tasks
import os
from dotenv import load_dotenv
import datetime
import pytz
import numpy as np
from MABInstance import MABInstance
# Get bot token:
load_dotenv()
BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")
# Set thread section and interaction timeout duration:
thread_section_duration = 60 * 24 # (mins & hours) e.g. 24 hours, 1 day
# Enable the message_content intent
intents = discord.Intents.all()
# Set bot command prefix: e.g. /hello
bot = commands.Bot(command_prefix = '/', intents = intents)
# When the bot is online, show a short message:
@bot.event
async def on_ready():
print("Bot is online!")
# When new member joins the server, send out a greeting message:
@bot.event
async def on_member_join(member):
# Get the text channels the member is in
member_text_channels = [channel for channel in member.guild.text_channels if member in channel.members]
if member_text_channels:
# Use the first text channel the member is in:
channel = member_text_channels[0]
else:
# Otherwise, uses the main text channel:
channel = member.guild.system_channel
# Send message:
if channel is not None:
message = f'{member.display_name} has joined the server!\n\nPlease type "/suggest" to start your suggestion section.'
await channel.send(message)
# /suggest command:
@bot.command()
async def suggest(ctx):
command_generator(ctx)
active_tasks = [] # record all started tasks
# creates tasks to allow concurrency:
def command_generator(ctx):
# start the thread:
task = tasks.loop(seconds=1)(suggestion_thread_loop)
active_tasks.append(task)
task.start(ctx, task)
# suggest thread loop:
async def suggestion_thread_loop(ctx, task):
# create thread for the user:
utc_now = datetime.datetime.utcnow()
pacific = pytz.timezone('US/Pacific')
pdt_now = pacific.fromutc(utc_now)
curr_time = pdt_now.strftime("%Y-%m-%d %H:%M")
thread = await ctx.channel.create_thread(
name = f"{curr_time}: Recommendation channel for <{ctx.author.display_name}>",
auto_archive_duration = thread_section_duration
)
# add user:
await thread.add_user(ctx.author)
# Tells the user where to start the recommendation process:
await ctx.send(f"For <{ctx.author.display_name}>: Click here to have your customized recommendation {thread.mention}")
# check user's response:
def check(response):
return response.author == ctx.author and response.channel == thread and not thread.locked and not thread.archived
# create mab instance:
mab_instance = MABInstance(ctx.author.id, True, "discord")
# get contexts:
contexts = mab_instance.get_contexts()
# send initial suggestions message:
message = "Which of the following scenarios are you in right now:\n"
for i, context in enumerate(contexts, start = 1):
message += f"\n{i}. {context}\n"
message += "\nType in the chat with the number of your choice."
await thread.send(message)
# get context response from user:
while True:
context_response = await bot.wait_for('message', check=check)
try:
context_index = int(context_response.content) - 1
if 0 <= context_index < len(contexts):
break
else:
await thread.send(f"Uh-oh...Please select a context with a number input from 1 to {len(contexts)}.")
except ValueError:
if context_response.content == "/suggest":
await thread.send("Please return to the main chat channel to start a new recommendation session.")
else:
await thread.send(f"Sorry we only accept numbers for context selection. Please select a context with a number input from 1 to {len(contexts)}.")
# send suggestions and wait for user's reaction to suggestion
first_round = True
fail_to_suggest = False
prev_sugg_indices = [] # record all suggestions indices given to the user
suggestions = mab_instance.get_suggestions()
while True:
sugg_list = mab_instance.make_recommendation(context_index, prev_sugg_indices)
# there are no more suggestions, break the loop:
if len(sugg_list) == 0:
fail_to_suggest = True
break
sugg_indices = np.where(np.isin(suggestions, sugg_list))[0]
prev_sugg_indices = np.unique(np.concatenate((prev_sugg_indices, sugg_indices)).astype(int))
if first_round:
first_round = False
message = "Please wait a moment while we fetch your suggestions..."
message += "\n\nYay! Here they are. Choose what you want to do most right now.\n\nIf you don't like any of the recommendations, simply reply 0.\n"
for i, sugg in enumerate(sugg_list, start = 1):
message += f"\n{i}. {sugg}\n"
await thread.send(message)
else:
message = "Don't worry! Let's try this again, One moment please..."
message += "\n\nYay! Here they are. Choose what you want to do most right now.\n\nIf you don't like any of the recommendations, simply reply 0.\n"
for i, sugg in enumerate(sugg_list, start = 1):
message += f"\n{i}. {sugg}\n"
await thread.send(message)
# get user response for recommendations:
while True:
suggestion_response = await bot.wait_for('message', check=check)
try:
sugg_idx = int(suggestion_response.content) - 1
if -1 <= sugg_idx < len(sugg_list):
break
else:
await thread.send(f"Uh-oh...Please select an activity with a number input from 0 to {len(sugg_list)}.")
except ValueError:
if suggestion_response.content == "/suggest":
await thread.send("Please return to the main chat channel to start a new recommendation session.")
else:
await thread.send(f"Sorry we only accept numbers for activity selection. Please select an activity with a number input from 0 to {len(sugg_list)}.")
if sugg_idx == -1:
suggestion_index = -1
else:
suggestion_index = mab_instance.get_suggestion_index(sugg_list[sugg_idx])
break
# based on whether made a successful suggestion, behave properly:
if fail_to_suggest:
feedback_rating = -1
await thread.send("Oops...Looks like you didn't like all the activities we just gave you.\n\nWe will add more options, please come back next time!")
else:
# send instruction & image for activity and get user feedback:
with open(mab_instance.get_suggestion_Image(suggestion_index), 'rb') as image_file:
sugg_image = discord.File(image_file)
await thread.send(file=sugg_image)
await thread.send("\n\u200b")
message = f"Great! {mab_instance.get_suggestion_description(suggestion_index)}"
message += "\n\nTake your time! Once you are done, please provide a feedback from 0 (unhelpful) to 5 (out of this world) so we can better help you next time!"
await thread.send(message)
while True:
feedback_response = await bot.wait_for('message', check=check)
try:
feedback_rating = int(feedback_response.content)
if 0 <= feedback_rating <= 5:
break
else:
await thread.send("Uh-oh...Please give us a feedback from 0 to 5.")
except ValueError:
if feedback_response.content == "/suggest":
await thread.send("Please return to the main chat channel to start a new recommendation session.")
else:
await thread.send("Sorry we only accept numbers for feedback. Please give us a feedback from 0 to 5.")
# Last bot message for this thread:
if feedback_rating == 0:
await thread.send("Uh-oh... I'm sorry this activity isn't helpful to you. We will add more activities. Thank you for your time and patience.")
elif feedback_rating <= 3:
await thread.send("That's great! I'm glad you tried our activity. We will make further improvements. Until next time!")
else:
await thread.send("Excellent! I'm glad our activity helped. Have a nice day!")
# update activity in both user's own activity history, and the total activity history
utc_now = datetime.datetime.utcnow()
pacific = pytz.timezone('US/Pacific')
pdt_now = pacific.fromutc(utc_now)
curr_time = pdt_now.strftime("%Y-%m-%d %H:%M:%S")
mab_instance.update_activity_log(curr_time, context_index, suggestion_index, feedback_rating)
# cancel task:
task.cancel()
# update data & history data file:
mab_instance.update_mab_file(context_index, suggestion_index, feedback_rating, prev_sugg_indices)
# close database connection:
mab_instance.close_db_connection()
# Run the bot:
bot.run(BOT_TOKEN)