Skip to content

Commit db8d4bb

Browse files
authored
Input handling (#1)
* Writting message to channel specified in config.json * Changed real botToken to an example one for safety reasons * Sending data from modal to config specified channel * Preventing unauthorized users from using the /event command * Added roleId to exampleConfig.json * Added buttons for @everyone pings because discord doesn't allow them in modals Also added requirements.txt * Ignoring users without permission * replaced json with .env files * leaked token again had to remove .env from repo * updated requirements.txt * updated example.env * changed .env variables to snake case * removed unnecessary import json
1 parent 8f94172 commit db8d4bb

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

requirements.txt

530 Bytes
Binary file not shown.

src/Bot.py

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import discord
2+
import os
3+
from dotenv import load_dotenv
4+
from discord.ext import commands
5+
6+
7+
load_dotenv()
8+
bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
9+
10+
11+
class EveryonePromptView(discord.ui.View):
12+
13+
def __init__(self, modal_input: dict):
14+
super().__init__(timeout=None)
15+
self.modal_input = modal_input
16+
17+
@discord.ui.button(label="@everyone ???", style=discord.ButtonStyle.success)
18+
async def set_bool_true(self, interaction: discord.Interaction, self_item) -> None:
19+
await send_event(self.modal_input, True)
20+
await interaction.response.send_message(
21+
f"Event {self.modal_input.get('meetingTitle')} sent to the channel successfully", ephemeral=True)
22+
self.stop()
23+
24+
@discord.ui.button(label="No ping", style=discord.ButtonStyle.red)
25+
async def set_bool_false(self, interaction: discord.Interaction, self_item) -> None:
26+
await send_event(self.modal_input, False)
27+
await interaction.response.send_message(
28+
f"Event {self.modal_input.get('meetingTitle')} sent to the channel successfully", ephemeral=True)
29+
self.stop()
30+
31+
32+
class EventModal(discord.ui.Modal, title="Quack"):
33+
meetingTitle = discord.ui.TextInput(label="Meeting title", placeholder="reversing?", required=True)
34+
description = discord.ui.TextInput(label="Meeting description", placeholder="???", required=True)
35+
date = discord.ui.TextInput(label="What time is the meeting?", placeholder="Hope it isn't monday", required=True)
36+
room = discord.ui.TextInput(label="Do we have a place to sit?", placeholder="EITI 133?", required=True)
37+
beer = discord.ui.TextInput(label="BEER?", placeholder="I hope so", required=True)
38+
39+
async def on_submit(self, interaction: discord.Interaction):
40+
modal_input = dict(meetingTitle=self.meetingTitle.value,
41+
description=self.description.value,
42+
date=self.date.value,
43+
room=self.room.value,
44+
beer=self.beer.value)
45+
view = EveryonePromptView(modal_input=modal_input)
46+
await interaction.response.send_message(f"Event {self.meetingTitle} added successfully",
47+
ephemeral=True,
48+
view=view)
49+
await view.wait()
50+
51+
52+
# Example of an event
53+
@bot.event
54+
async def on_ready():
55+
print(f'Logged on as {bot.user}')
56+
try:
57+
synced = await bot.tree.sync()
58+
print(f'Synced {len(synced)} commands')
59+
except Exception as e:
60+
print(e)
61+
62+
63+
@bot.tree.command(name="event")
64+
async def event(interaction: discord.Interaction):
65+
if interaction.guild.get_role(int(os.getenv("role_id"))) in interaction.user.roles:
66+
await interaction.response.send_modal(EventModal())
67+
68+
69+
async def send_event(modal_input, everyone):
70+
event_channel = bot.get_channel(int(os.getenv("event_channel_id")))
71+
event_message = create_event_message(modal_input=modal_input, everyone=everyone)
72+
await event_channel.send(event_message)
73+
74+
75+
def create_event_message(modal_input: [], everyone):
76+
print(modal_input)
77+
modal_input.get("meetingTitle")
78+
isEveryone = "@everyone \t" if everyone else ""
79+
return f"{isEveryone}{modal_input.get('meetingTitle')}\n{modal_input.get('description')}\n\n\n{modal_input['room']},\t{modal_input['date']}\n\n{modal_input['beer']}\n\n"
80+
81+
82+
bot.run(os.getenv("token"))

src/example.env

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
event_channel_id = 1110659456175460372
2+
role_id = 1110685519362412574

0 commit comments

Comments
 (0)