This repository was archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.py
More file actions
149 lines (125 loc) · 4.69 KB
/
bot.py
File metadata and controls
149 lines (125 loc) · 4.69 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
146
147
148
149
import discord
from discord.ext import commands
from discord_slash import SlashCommand, SlashContext
from discord_slash.model import SlashCommandOptionType
from discord_slash.utils.manage_commands import create_option
import datetime
from dotenv import load_dotenv
import os
import requests
# initializers
load_dotenv()
DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
IDEA_TOKEN = os.getenv('IDEA_TOKEN')
BASE_URL = os.getenv('BASE_URL') # root URL for requests (including '/' at end)
GUILD_IDS = [int(os.getenv('GUILD_IDS',0))]
head = {'Authorization': 'Token ' + IDEA_TOKEN}
intents = discord.Intents.all()
intents.members = True
bot = commands.Bot(command_prefix="!", description=DESCRIPTION,intents=intents)
slash = SlashCommand(bot,sync_commands=True)
###################################
# database manipulation functions #
###################################
class InvalidAccess(Exception):
"""You do not have write permissions for this idea"""
pass
def insertIdea(title, description, author):
new_idea = {
'title': title,
'description': description,
'author': author,
'approved': False
}
idea = requests.get(
f"{BASE_URL}idealab/ideas/?title={title.replace(' ', '%20')}",
headers=head
).json()
if idea['count'] != 0:
raise InvalidAccess
# check for updates here
else:
response = requests.post(url=f'{BASE_URL}idealab/ideas/',
json=new_idea, headers=head)
return(response)
def allIdeas():
idea = requests.get(f"{BASE_URL}idealab/ideas/").text
return idea
def removeIdea(title):
response = requests.delete(
f"{BASE_URL}idealab/ideas/?title={title.replace(' ', '%20')}",
headers=head)
return response.status_code
###################################
# bot commands and initialization #
###################################
@bot.event
async def on_ready():
print('logged in as ' + str(bot.user.name))
@slash.slash(name="help", description='Help Command', guild_ids=GUILD_IDS)
async def help(ctx: SlashContext):
embed = discord.Embed(title='Help')
embed.add_field(name='$add [idea]', value='add an idea', inline=True)
embed.add_field(name='$edit [idea]', value='edit an idea', inline=True)
embed.add_field(name='$remove [idea]', value='remove an idea (only available to idea author and admins)', inline=False)
await ctx.send(embed=embed)
@slash.slash(name='add',
description='Add an idea',
guild_ids=GUILD_IDS,
options=[
create_option(
name='title',
description='the title of your idea',
option_type=SlashCommandOptionType.STRING,
required=True
),
create_option(
name='description',
description='a description of your idea',
option_type=SlashCommandOptionType.STRING,
required=True
),
create_option(
name='name',
description='your name',
option_type=SlashCommandOptionType.STRING,
required=True
),
])
async def add(ctx: SlashContext, title: str, description: str, name: str):
insertIdea(title, description, name)
await ctx.send(content=(f'{name} sucessfully added "{title}"'))
@slash.slash(name='edit',
description='Edit an idea',
guild_ids=GUILD_IDS,
options=[
create_option(
name='title',
description='the title of your idea',
option_type=SlashCommandOptionType.STRING,
required=True
),
])
async def edit(ctx: SlashContext, title: str, description: str, name: str):
insertIdea(title, description, name)
await ctx.send(content=(f'{ctx.author.nick} sucessfully edited "{title}"'))
@slash.slash(name='all', description='List All Ideas', guild_ids=GUILD_IDS)
async def all(ctx: SlashContext):
await ctx.send(content=allIdeas())
@slash.slash(name='remove',
description='Remove an idea',
options=[
create_option(
name='title',
description='the title of your idea',
option_type=SlashCommandOptionType.STRING,
required=True
),
])
async def remove(ctx: SlashContext, title: str):
response = removeIdea(title)
if response == 200:
await ctx.send(content=f'Idea "{title}" removed')
else:
await ctx.send(content='Error' + response)
bot.run(DISCORD_TOKEN)