-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
176 lines (147 loc) · 6.33 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import discord
import asyncio
from discord.ext import commands
import json
import random
import openai
ListColours = [
0xbed5e7,
0xb0cce1,
0x9ec4db,
0xffd1dc,
0xffb6c1,
0xf46fa8,
0x9dc6ca,
0xffea95,
0xffa88c,
0x77DD77,
0x836953,
0x89cff0,
0x99c5c4,
0x9adedb,
0xaa9499,
0xaaf0d1
]
def read_bot_token():
with open("secrets.json") as f:
data = json.load(f)
return data["DISCORD_TOKEN"]
def read_openai_token():
with open("secrets.json") as f:
data = json.load(f)
return data["OPENAI_TOKEN"]
openai.api_key= read_openai_token()
bot=commands.Bot(
command_prefix="!",
intents=discord.Intents.all(),
help_command=None,
enable_debug_events = True
)
@bot.event
async def on_ready():
print(f'Logged in as: {bot.user}')
await bot.load_extension("music")
servers = len(bot.guilds)
await bot.change_presence(status=discord.Status.online, activity=discord.Game(f"in {servers} servers!"))
@bot.command()
async def help(ctx):
embed1 = discord.Embed(title="Help", description="**General Commands (Page 1):**\n Press ▶️ to go to Music Commands.", color=random.choice(ListColours))
embed1.add_field(name="!ping", value="```Returns Pong!```", inline=False)
embed1.add_field(name="!say", value="```Echoes your message```", inline=False)
embed1.add_field(name="!help", value="```Help Menu```", inline=False)
embed1.add_field(name="!remindme", value="```Reminds you after a given time```", inline=False)
embed1.add_field(name="!gptchannel", value="```Sets the GPT-3 chat channel```", inline=False)
embed2 = discord.Embed(title="Help", description="**Music Commands (Page 2):**\n Press ◀️ to go to General Commands.", color=random.choice(ListColours))
embed2.add_field(name="!play", value="```Plays a song```", inline=False)
embed2.add_field(name="!skip", value="```Skips the current song```", inline=False)
embed2.add_field(name="!pause", value="```Pauses the current song```", inline=False)
embed2.add_field(name="!resume", value="```Resumes the current song```", inline=False)
embed2.add_field(name="!queue", value="```Shows the queue```", inline=False)
embed2.add_field(name="!stop", value="```Stops the music```", inline=False)
embed2.add_field(name="!nowplaying", value="```Shows the current song```", inline=False)
embed2.add_field(name="!disconnect", value="```Disconnects the bot```", inline=False)
pages = [embed1, embed2]
page_number = 0
message = await ctx.send(embed=pages[page_number])
await message.add_reaction('◀️')
await message.add_reaction('▶️')
def check(reaction, user):
return user == ctx.author and str(reaction.emoji) in ['◀️', '▶️']
while True:
try:
reaction, user = await bot.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
await message.clear_reactions()
break
if str(reaction.emoji) == '▶️' and page_number != len(pages) - 1:
page_number += 1
await message.edit(embed=pages[page_number])
await message.remove_reaction(reaction, user)
elif str(reaction.emoji) == '◀️' and page_number > 0:
page_number -= 1
await message.edit(embed=pages[page_number])
await message.remove_reaction(reaction, user)
else:
await message.remove_reaction(reaction, user)
@bot.command()
async def ping(ctx):
embed=discord.Embed(title="Ping", description=f"🏓 Pong! {round(bot.latency * 1000)}ms", color=random.choice(ListColours))
await ctx.reply(embed=embed)
@bot.command()
async def say(ctx, *, message=None):
if not message:
embed=discord.Embed(title="Say", description="Please provide a message after the command. \n**Usage:** ```!say [message]```", color=random.choice(ListColours))
await ctx.reply(embed=embed)
return
embed=discord.Embed(title="Say", description=message, color=random.choice(ListColours))
await ctx.reply(embed=embed)
@bot.command()
async def remindme(ctx, time=None, *, message=None):
if not time or not message:
embed=discord.Embed(title="Say", description="You must provide a time and a message. \n**Example:** ```!remindme 1d do laundry```", color=random.choice(ListColours))
await ctx.reply(embed=embed)
return
seconds = 0
if time.lower().endswith("d"):
seconds += int(time[:-1]) * 60 * 60 * 24
counter = f"{seconds // 60 // 60 // 24} days"
if time.lower().endswith("h"):
seconds += int(time[:-1]) * 60 * 60
counter = f"{seconds // 60 // 60} hours"
elif time.lower().endswith("m"):
seconds += int(time[:-1]) * 60
counter = f"{seconds // 60} minutes"
elif time.lower().endswith("s"):
seconds += int(time[:-1])
counter = f"{seconds} seconds"
embed=discord.Embed(title="Remind Me", description=f"Okay, I will remind you about **\"{message}\" in {counter}**.", color=random.choice(ListColours))
await ctx.reply(embed=embed)
await asyncio.sleep(seconds)
embed=discord.Embed(title="Reminder", description=f"You asked me to remind you about **\"{message}\", {counter} ago**. ", color=random.choice(ListColours))
await ctx.send(f"Hey {ctx.author.mention},", embed=embed)
gpt3_channel_id = None
@bot.command()
async def gptchannel(ctx, channel: discord.TextChannel):
global gpt3_channel_id
gpt3_channel_id = channel.id
await ctx.reply(f"GPT-3 chat channel set to <#{channel.id}>.")
@bot.event
async def on_message(message):
global gpt3_channel_id
if gpt3_channel_id:
if message.channel.id == int(gpt3_channel_id) and message.author != bot.user:
response = openai.Completion.create(
model="text-davinci-003",
prompt=message.content,
temperature=0.33,
max_tokens=50,
presence_penalty=0,
frequency_penalty=0,
best_of=1,
)
bot_response = response.choices[0].text.strip()
embed=discord.Embed(title="GPT-3 Chat (text-davinci-003 model)", description=bot_response, color=random.choice(ListColours))
await message.channel.send(embed=embed)
await bot.process_commands(message)
token = read_bot_token()
bot.run(token)