Skip to content

Commit b4d7802

Browse files
authored
Merge pull request #297 from practical-python-org/deploy
fix: Line ending enforcement and auto-deployment
2 parents 37843cc + c80b183 commit b4d7802

File tree

5 files changed

+142
-98
lines changed

5 files changed

+142
-98
lines changed

.editorconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
indent_size = 2
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.py]
12+
charset = utf-8
13+
indent_style = space
14+
indent_size = 4
15+
end_of_line = lf
16+
insert_final_newline = true
17+
trim_trailing_whitespace = true
18+
19+
[*.{md,mdx}]
20+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
* text=auto eol=lf
12
src/zorak/_version.py export-subst

.github/workflows/deploy.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Deploy to EC2
2+
3+
on:
4+
push:
5+
branches:
6+
- Main
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v2
14+
15+
- name: SSH and Deploy
16+
run: |
17+
ssh -o StrictHostKeyChecking=no -i ${{ secrets.SSH_KEY }} ${{ secrets.USERNAME }}@${{ secrets.HOST }} << 'ENDSSH'
18+
cd /srv/ZorakBot
19+
sudo git pull
20+
docker-compose up -d --build zorak_bot_prod
21+
ENDSSH
22+
env:
23+
SSH_KEY: ${{ secrets.SSH_KEY }}

requirements.txt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
py-cord
2-
beautifulsoup4
3-
requests
4-
matplotlib
5-
DateTime
6-
pytz
7-
pistonapi
8-
toml
9-
feedparser
10-
html2text
11-
pymongo
12-
PyNaCl==1.5.0
13-
ffmpeg-python==0.2.0
14-
yt-dlp==2023.11.16
15-
googletrans==3.1.0a0
1+
py-cord
2+
beautifulsoup4
3+
requests
4+
matplotlib
5+
DateTime
6+
pytz
7+
pistonapi
8+
toml
9+
feedparser
10+
html2text
11+
pymongo
12+
PyNaCl==1.5.0
13+
ffmpeg-python==0.2.0
14+
yt-dlp==2023.11.16
15+
googletrans==3.1.0a0
1616
dnspython==2.3.0
Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,83 @@
1-
"""
2-
A trivia command.
3-
"""
4-
import logging
5-
import json
6-
from discord.ui.item import Item
7-
import requests
8-
import urllib
9-
import html
10-
from discord.ext import commands
11-
import discord
12-
import random
13-
logger = logging.getLogger(__name__)
14-
15-
class GeneralTrivia(commands.Cog):
16-
"""
17-
# Hits the trivia API and returns the response
18-
"""
19-
20-
def __init__(self, bot):
21-
self.bot = bot
22-
23-
@commands.slash_command()
24-
async def trivia_cmd(self, ctx):
25-
"""
26-
Sends a trivia using an API
27-
"""
28-
logger.info("%s used the %s command."
29-
, ctx.author.name
30-
, ctx.command)
31-
req = requests.get("https://opentdb.com/api.php?amount=1&category=18&difficulty=medium&type=multiple")
32-
if req.json()['response_code'] == 0:
33-
34-
await ctx.respond(html.unescape(req.json()['results'][0]['question']))
35-
view = discord.ui.View()
36-
answer_list = []
37-
for i in req.json()['results'][0]['incorrect_answers']:
38-
answer_list.append((i,False))
39-
answer_list.append((req.json()['results'][0]['correct_answer'],True))
40-
al = answer_list[:]
41-
correct_ans = len(answer_list)-1
42-
button_list = []
43-
async def empty(interaction:discord.Interaction):
44-
pass
45-
async def correct(interaction:discord.Interaction):
46-
if interaction.user == ctx.author:
47-
await interaction.response.defer()
48-
await interaction.followup.send(content="The answer is correct, good job human")
49-
for i in button_list:
50-
i.callback = empty
51-
52-
async def wrong(interaction:discord.Interaction):
53-
if interaction.user == ctx.author:
54-
await interaction.response.defer()
55-
await interaction.followup.send(content=f"Wrong answer, the answer was {answer_list[correct_ans][0]}")
56-
await interaction.edit_original_response()
57-
for i in button_list:
58-
i.callback = empty
59-
for i in range(correct_ans+1):
60-
index = random.randint(0,len(al)-1)
61-
label = html.unescape(al[index][0])
62-
button = discord.ui.Button(label=label)
63-
button_list.append(button)
64-
#print(button.callback)
65-
if al[index][1]:
66-
67-
button.callback = correct
68-
else:
69-
button.callback = wrong
70-
del al[index]
71-
view.add_item(button)
72-
msg = await ctx.send(view=view)
73-
74-
else:
75-
await ctx.respond('Oops the server seems to have made a mistake, try the command again')
76-
77-
78-
79-
def setup(bot):
80-
"""
81-
Required.
82-
"""
83-
bot.add_cog(GeneralTrivia(bot))
1+
"""
2+
A trivia command.
3+
"""
4+
import logging
5+
import json
6+
from discord.ui.item import Item
7+
import requests
8+
import urllib
9+
import html
10+
from discord.ext import commands
11+
import discord
12+
import random
13+
logger = logging.getLogger(__name__)
14+
15+
class GeneralTrivia(commands.Cog):
16+
"""
17+
# Hits the trivia API and returns the response
18+
"""
19+
20+
def __init__(self, bot):
21+
self.bot = bot
22+
23+
@commands.slash_command()
24+
async def trivia_cmd(self, ctx):
25+
"""
26+
Sends a trivia using an API
27+
"""
28+
logger.info("%s used the %s command."
29+
, ctx.author.name
30+
, ctx.command)
31+
req = requests.get("https://opentdb.com/api.php?amount=1&category=18&difficulty=medium&type=multiple")
32+
if req.json()['response_code'] == 0:
33+
34+
await ctx.respond(html.unescape(req.json()['results'][0]['question']))
35+
view = discord.ui.View()
36+
answer_list = []
37+
for i in req.json()['results'][0]['incorrect_answers']:
38+
answer_list.append((i,False))
39+
answer_list.append((req.json()['results'][0]['correct_answer'],True))
40+
al = answer_list[:]
41+
correct_ans = len(answer_list)-1
42+
button_list = []
43+
async def empty(interaction:discord.Interaction):
44+
pass
45+
async def correct(interaction:discord.Interaction):
46+
if interaction.user == ctx.author:
47+
await interaction.response.defer()
48+
await interaction.followup.send(content="The answer is correct, good job human")
49+
for i in button_list:
50+
i.callback = empty
51+
52+
async def wrong(interaction:discord.Interaction):
53+
if interaction.user == ctx.author:
54+
await interaction.response.defer()
55+
await interaction.followup.send(content=f"Wrong answer, the answer was {answer_list[correct_ans][0]}")
56+
await interaction.edit_original_response()
57+
for i in button_list:
58+
i.callback = empty
59+
for i in range(correct_ans+1):
60+
index = random.randint(0,len(al)-1)
61+
label = html.unescape(al[index][0])
62+
button = discord.ui.Button(label=label)
63+
button_list.append(button)
64+
#print(button.callback)
65+
if al[index][1]:
66+
67+
button.callback = correct
68+
else:
69+
button.callback = wrong
70+
del al[index]
71+
view.add_item(button)
72+
msg = await ctx.send(view=view)
73+
74+
else:
75+
await ctx.respond('Oops the server seems to have made a mistake, try the command again')
76+
77+
78+
79+
def setup(bot):
80+
"""
81+
Required.
82+
"""
83+
bot.add_cog(GeneralTrivia(bot))

0 commit comments

Comments
 (0)