-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
161 lines (123 loc) · 5.12 KB
/
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
"""
A discord bot which responds to messages by generating a response using a GPT model.
When a message is sent in a channel with a channel id in the list CHANNEL_IDS, the bot will generate a response and respond to the message
The bot should import the token from token.txt
The bot should import the list of channels (CHANNEL_IDS) from channels.txt
"""
import discord
from discord.ext import commands
from transformers import pipeline, set_seed
import random
import names
from better_profanity import profanity
import asyncio
import logging
# Imports the token from token.txt
with open("token.txt", "r") as token_file:
TOKEN = token_file.read().strip()
# Imports the list of channels from channels.txt
with open("channels.txt", "r") as channels_file:
CHANNEL_IDS = [int(i) for i in channels_file.read().strip().split("\n")]
# Imports the owner's id from owner.txt
with open("owner.txt", "r") as owner_file:
OWNER_ID = owner_file.read().strip()
# Imports a list of prompts from prompts.txt
with open("prompts.txt", "r") as prompts_file:
PROMPTS = prompts_file.read().strip().split("\n")
# Sets the seed for the transformers library
set_seed(random.randint(0, 1000000))
class Respond_to_message(commands.Cog):
"""
A class which contains the bot's commands
"""
def __init__(self, bot):
self.bot = bot
self.model = pipeline('text-generation', model='gpt2')
@commands.command(name="generate_response", help="Generates a response to a message", aliases=["gr"])
async def generate_response(self, ctx, *, message: str):
"""
Generates a response to the given message
:param ctx: The context of the command
:param message: The message to respond to
:return: None
"""
# Imports a list of prompts from prompts.txt
with open("prompts.txt", "r") as prompts_file:
PROMPTS = prompts_file.read().strip().split("\n")
async with ctx.channel.typing():
# Generates a response
response = generate_response(message, self.model)
# Responds to the message
await ctx.reply(response)
# The bot will respond to the most recent message in any of the channels listed
@commands.Cog.listener()
async def on_message(self, message):
"""
The bot will respond to any messages in CHANNEL_IDS
:param message: The message to respond to
:return: None
"""
if message.author == self.bot.user:
return
c_id = message.channel.id
if c_id in CHANNEL_IDS or message.channel.type == discord.ChannelType.private:
async with message.channel.typing():
# Generates a response
response = generate_response(message.content, self.model)
# Responds to the message
await message.reply(response)
def generate_response(message: str, the_pipeline: pipeline, recursion_count: int = 0) -> str:
"""
Generates a response to the given message
:param message: The message to respond to
:param the_pipeline: The pipeline to use for the generation
:return: The generated response
"""
if recursion_count > 10:
return "Error: could not generate a proper response!"
# Creates a dictionary with the message and the context
full_context_string = random.choice(PROMPTS).replace("INPUT", profanity.censor(message)).replace("\\n", "\n")
while "NAME" in full_context_string:
full_context_string = full_context_string.replace("NAME", names.get_first_name(), 1)
# Generates a response using the model
response = the_pipeline(full_context_string, max_length=100, num_return_sequences=1)
response = response[0]['generated_text']
if(contains_bad_words(response)):
return generate_response(message, the_pipeline, recursion_count + 1)
# Append the response to responses.txt
with open("responses.txt", "a") as responses_file:
responses_file.write(response + "\n---\n")
response = response[len(full_context_string):]
response = response[:response.find("\"")]
if(response == ""):
return generate_response(message, the_pipeline, recursion_count + 1)
# Returns the response
return response
def contains_bad_words(response: str) -> bool:
"""
Checks if the response contains a bad word
:param response: The response to check
:return: True if the response contains a bad word, False otherwise
"""
return profanity.contains_profanity(response)
print("hi")
# Sets up the bot
intents = discord.Intents.default()
intents.guilds = True
intents.message_content = True
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def on_ready():
await bot.add_cog(Respond_to_message(bot))
asyncio.create_task(bot.tree.sync())
print("Started!")
try:
import winsound
duration = 1000 # milliseconds
freq = 440 # Hz
winsound.Beep(freq, duration)
except Exception:
print("no beep")
# Starts the bot
bot.run(TOKEN, log_handler=handler)