Skip to content

Python Version #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitignore

This file was deleted.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# starter-discord-bot

I remade this in python because i felt like it uwu
Follow these instructions after deploying this repo on Cyclic.sh

[![Deploy to Cyclic](https://deploy.cyclic.app/button.svg)](https://deploy.cyclic.app/)
Expand Down
70 changes: 70 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import os
import requests
from flask import Flask, request, jsonify
from discord_interactions import verify_key_decorator, InteractionType, InteractionResponseType

APPLICATION_ID = os.getenv('APPLICATION_ID')
TOKEN = os.getenv('TOKEN')
PUBLIC_KEY = os.getenv('PUBLIC_KEY', 'not set')
GUILD_ID = os.getenv('GUILD_ID')

discord_api = requests.Session()
discord_api.headers.update({
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE",
"Access-Control-Allow-Headers": "Authorization",
"Authorization": f"Bot {TOKEN}"
})

app = Flask(__name__)

@app.route('/interactions', methods=['POST'])
@verify_key_decorator(PUBLIC_KEY)
def interactions():
interaction = request.json

if interaction["type"] == InteractionType.APPLICATION_COMMAND:
if interaction["data"]["name"] == "yo":
return jsonify({
"type": InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
"data": {"content": f"Yo {interaction['member']['user']['username']}!"}
})

if interaction["data"]["name"] == "dm":
c = discord_api.post("https://discord.com/api/users/@me/channels", json={
"recipient_id": interaction["member"]["user"]["id"]
}).json()

try:
discord_api.post(f"https://discord.com/api/channels/{c['id']}/messages", json={
"content": "Yo! I got your slash command. I am not able to respond to DMs just slash commands."
})
except Exception as e:
print(e)

return jsonify({
"type": InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
"data": {"content": "👍"}
})

@app.route('/register_commands', methods=['GET'])
def register_commands():
slash_commands = [
{"name": "yo", "description": "replies with Yo!", "options": []},
{"name": "dm", "description": "sends user a DM", "options": []}
]
try:
discord_response = discord_api.put(
f"https://discord.com/api/applications/{APPLICATION_ID}/guilds/{GUILD_ID}/commands",
json=slash_commands
)
return "commands have been registered"
except requests.exceptions.RequestException as e:
return f"{e.response.status_code} error from discord"

@app.route('/', methods=['GET'])
def home():
return "Follow documentation"

if __name__ == '__main__':
app.run(port=8999)
113 changes: 0 additions & 113 deletions index.js

This file was deleted.

Loading