forked from ckybb/CTFtime-DiscordBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
137 lines (119 loc) · 6.43 KB
/
bot.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
import discord
from discord import app_commands
import json
import requests
import time
import datetime
import zoneinfo
import config
header = {"User-Agent": "Mozilla/5.0", "content-type": "application/json"}
dd = datetime.datetime
JST = zoneinfo.ZoneInfo('Asia/Tokyo')
intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
evecfg = False
@tree.command(name='eventcfg', description='eventコマンドのdescriptionを有効にするか設定します。')
async def set_eventcfg(interaction: discord.Interaction, value: bool):
evecfg = value
if evecfg:
await interaction.response.send_message('descriptionを有効にしました。')
else:
await interaction.response.send_message('descriptionを無効にしました。')
@tree.command(name='event', description='現在から一週間後までで開催予定のCTF一覧を取得します。(最大10件)')
async def get_event(interaction: discord.Interaction):
await interaction.response.defer()
tstamp = int(time.time())
res = requests.get(f'https://ctftime.org/api/v1/events/?limit=10&start={tstamp}&finish={tstamp+604800}', headers=header)
if res.status_code != 200:
await interaction.followup.send(f'取得できませんでした。status_code: {res.status_code}')
else:
data = res.json()
n = len(data)
embeds = []
for i in range(n):
starttime = dd.fromisoformat(data[i]["start"]).astimezone(JST)
finishtime = dd.fromisoformat(data[i]["finish"]).astimezone(JST)
if evecfg:
embed = discord.Embed(title=data[i]["title"], color=0x00ff00, description=data[i]["description"][:2048], url=data[i]["url"])
else:
embed = discord.Embed(title=data[i]["title"], color=0x00ff00, url=data[i]["url"])
embed.set_thumbnail(url=data[i]["logo"])
embed.add_field(name='主催', value=data[i]["organizers"][0]["name"])
if data[i]["location"] == "":
embed.add_field(name='Location', value="On-Line")
else:
embed.add_field(name="Location", value=data[i]["location"])
embed.add_field(name="形式", value=data[i]["format"])
embed.add_field(name='開始時刻', value=starttime)
embed.add_field(name='終了時刻', value=finishtime)
embed.add_field(name='weight', value=data[i]["weight"])
embed.add_field(name='URL', value=data[i]["url"])
embed.add_field(name='URL(CTFtime)', value=data[i]["ctftime_url"])
embeds.append(embed)
await interaction.followup.send(f'{n}件開催予定です:\n', embeds=embeds)
@tree.command(name='description', description='CTF一覧の説明のみ表示します。')
async def get_description(interaction: discord.Interaction):
await interaction.response.defer()
tstamp = int(time.time())
res = requests.get(f'https://ctftime.org/api/v1/events/?limit=10&start={tstamp}&finish={tstamp+604800}', headers=header)
if res.status_code != 200:
await interaction.followup.send(f'取得できませんでした。status_code: {res.status_code}')
else:
data = res.json()
n = len(data)
embeds = []
for i in range(n):
embed = discord.Embed(title=data[i]["title"], color=0x00ff00, description=data[i]["description"][:2048], url=data[i]["url"])
embed.set_thumbnail(url=data[i]["logo"])
embeds.append(embed)
await interaction.followup.send(f'{n}件開催予定です:\n', embeds=embeds)
@tree.command(name='team', description='チームの情報を取得します。(RiST: 42506)')
async def get_team(interaction: discord.Interaction, id: int):
await interaction.response.defer()
res = requests.get(f'https://ctftime.org/api/v1/teams/{id}/', headers=header)
if res.status_code != 200:
await interaction.followup.send(f'取得できませんでした。status_code: {res.status_code}')
else:
data = res.json()
year = str(dd.now().year)
embed = discord.Embed(title=data["name"], color=0x00ff00, url=f'https://ctftime.org/team/{id}')
embed.set_thumbnail(url=data["logo"])
embed.add_field(name='Country', value=data["country"], inline=False)
embed.add_field(name='ID', value=data["id"], inline=False)
embed.add_field(name='世界順位', value=data["rating"][year]["rating_place"], inline=False)
embed.add_field(name='国内順位', value=data["rating"][year]["country_place"], inline=False)
embed.add_field(name='Rating', value=data["rating"][year]["rating_points"], inline=False)
await interaction.followup.send(embed=embed)
@tree.command(name='rist', description='RiSTの情報を取得します。')
async def get_rist(interaction: discord.Interaction):
await interaction.response.defer()
res = requests.get(f'https://ctftime.org/api/v1/teams/42506/', headers=header)
if res.status_code != 200:
await interaction.followup.send(f'取得できませんでした。status_code: {res.status_code}')
else:
data = res.json()
year = str(dd.now().year)
embed = discord.Embed(title=data["name"], color=0x00ff00, url=f'https://ctftime.org/team/42506')
embed.set_thumbnail(url=data["logo"])
embed.add_field(name='Country', value=data["country"], inline=False)
embed.add_field(name='ID', value=data["id"], inline=False)
embed.add_field(name='世界順位', value=data["rating"][year]["rating_place"], inline=False)
embed.add_field(name='国内順位', value=data["rating"][year]["country_place"], inline=False)
embed.add_field(name='Rating', value=data["rating"][year]["rating_points"], inline=False)
await interaction.followup.send(embed=embed)
@tree.command(name='shutdown', description='botを終了します。bot管理者のみ実行可能です。')
async def shutdown(interaction: discord.Interaction):
if interaction.user.id == int(config.ownerid):
await interaction.response.send_message('Botを停止します。')
print('received shutdown command.')
await client.close()
else:
await interaction.response.send_message('bot管理者しか実行できません。')
@client.event
async def on_ready():
print(f'Logged in as {client.user} (ID: {client.user.id})')
print('------')
await tree.sync()
#async def on_message():
client.run(config.token)