Skip to content

Commit

Permalink
Code clean up (staciax#58)
Browse files Browse the repository at this point in the history
* Cleaned imports

* Better error handler

Catching specific errors that might be raised.
added a super call to `close`
optimized imports

* Update auth.py

* bump 3.1.2

* some type hint

* Update auth.py

Co-authored-by: STACIA <[email protected]>
  • Loading branch information
RyugaXhypeR and staciax authored Jun 14, 2022
1 parent 07decb1 commit 8ddd586
Show file tree
Hide file tree
Showing 31 changed files with 2,440 additions and 2,356 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.pyc
.vscode
venv/
data/
data/
.idea/
36 changes: 24 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@
<!-- https://github.com/Cog-Creators/Red-DiscordBot -->

# About
Discord bot that shows your infomation and more without open the VALORANT by using the [In-game API.](https://github.com/HeyM1ke/ValorantClientAPI)

Discord bot that shows your infomation and more without open the VALORANT by using
the [In-game API.](https://github.com/HeyM1ke/ValorantClientAPI)
written using Python and the [Discord.py](https://github.com/Rapptz/discord.py) library <br>
For support using Valorant Bot, please join the [support server](https://discord.gg/RaCzsPnfNM)

## Heroku

[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy)

- [Youtube Tutorial](https://youtu.be/5ZFsEcDT8e4)

## Screenshot
Expand All @@ -67,7 +71,6 @@ For support using Valorant Bot, please join the [support server](https://discord
<img src="https://i.imgur.com/5jEZt3Z.png" alt="points">
</details>


## Installations

* [Python 3.8+](https://www.python.org/downloads/)
Expand All @@ -76,7 +79,9 @@ For support using Valorant Bot, please join the [support server](https://discord

* Install requirements

* **Create** the [discord bot](https://discord.com/developers/applications) and **Enable Privileged Gateway Intents** [`MESSAGE CONTENT INTENT`](https://i.imgur.com/TiiaYR9.png) then invite bot to server with scope [`applications.commands`](https://cdn.discordapp.com/attachments/939097458288496682/950613059150417970/IMG_3279.png)
* **Create** the [discord bot](https://discord.com/developers/applications) and **Enable Privileged Gateway
Intents** [`MESSAGE CONTENT INTENT`](https://i.imgur.com/TiiaYR9.png) then invite bot to server with
scope [`applications.commands`](https://cdn.discordapp.com/attachments/939097458288496682/950613059150417970/IMG_3279.png)

* Clone/[Download](https://github.com/staciax/ValorantStoreChecker-discord-bot/archive/refs/heads/master.zip)

Expand All @@ -91,15 +96,20 @@ pip install requests
pip install python-dotenv
```

* Store discord bot token and owner ID in [.env](https://github.com/staciax/ValorantStoreChecker-discord-bot/blob/master/.env)
* Store discord bot token and owner ID
in [.env](https://github.com/staciax/ValorantStoreChecker-discord-bot/blob/master/.env)

```
TOKEN='INPUT DISCORD TOKEN HERE'
OWNER_ID='INPUT YOUR DISCORD ID'
```

* Run the bot

```
python bot.py
```

* Slash Command is automatic global commands `(takes 1 hour to process.)`
* If you want to use commands in server right now `-sync guild` to sync the commands in your server.
* if you want remove commands in your server `-unsync guild` to remove server commands.
Expand Down Expand Up @@ -127,7 +137,9 @@ python bot.py
| `/debug` | command for debug `emoji`, `skin price`,`cache` is not loaded |

<!-- ## Translations (credit by [giorgio](https://github.com/giorgi-o) -->

## Translations

If you want to use your language and would like help translate the bot, please do!

- Option 1
Expand All @@ -142,29 +154,29 @@ If you want to use your language and would like help translate the bot, please d
4. Open that file and do the thing
5. Open a pull request

- Alternatively, you can just send me the JSON on discord and I'll upload it for you.
- Alternatively, you can just send me the JSON on discord and I'll upload it for you.

## Special Thanks

This project wouldn't have happened without.

- [HeyM1ke/ValorantClientAPI](https://github.com/RumbleMike/ValorantClientAPI)
for providing a great API about Valorant!
for providing a great API about Valorant!

- [colinhartigan/valclient.py](https://github.com/colinhartigan/valclient.py)
for beautiful client endpoint
for beautiful client endpoint

- [techchrism/valorant-api-docs](https://github.com/techchrism/valorant-api-docs/)
for API docs documentation
for API docs documentation

- [Valorant-API.com](https://valorant-api.com/)
for every skin names and images!
for every skin names and images!

- [github/giorgi-o](https://github.com/giorgi-o)
for always helping me and more!. ValoBot in JS [SkinPeek](https://github.com/giorgi-o/SkinPeek/)
for always helping me and more!. ValoBot in JS [SkinPeek](https://github.com/giorgi-o/SkinPeek/)

- [Discord - Valorant App Developer ](https://discord.gg/a9yzrw3KAm)
developer community for valorant api
developer community for valorant api

- [Contributors](https://github.com/staciax/ValorantStoreChecker-discord-bot/graphs/contributors) <3 <3

Expand All @@ -176,4 +188,4 @@ developer community for valorant api

<a href="https://tipme.in.th/renlyx">
<img link="https://ko-fi.com/staciax" src="https://static.tipme.in.th/img/logo.f8267020b29b.svg" width="170" />
</a>
</a>
32 changes: 22 additions & 10 deletions README_ภาษาไทย.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@
<!-- https://github.com/Cog-Creators/Red-DiscordBot -->

# เกี่ยวกับ

ดิสคอร์ดบอทที่โชว์ข้อมูลภายในเกมโดยไม่ต้องเข้าเกม โดยใช้ [In-game API](https://github.com/HeyM1ke/ValorantClientAPI)
เขียนโดย Python และ ไลบรารี่ [Discord.py](https://github.com/Rapptz/discord.py) <br>
ต้องการความช่วยเหลือสามารถเข้ามาใน [เซิฟเวอร์ดิสคอร์ด](https://discord.gg/RaCzsPnfNM)

## Heroku

[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy)

- [วีดีโอสอน](https://youtu.be/5ZFsEcDT8e4)

## ภาพประกอบ
Expand All @@ -67,16 +70,18 @@
<img src="https://i.imgur.com/5jEZt3Z.png" alt="points">
</details>


## วิธีติดตั่ง

* ดาวน์โหลด [Python 3.8+](https://www.python.org/downloads/)

* ดาวน์โหลด [Git](https://git-scm.com/downloads)

* [**สร้างบอทดิสคอร์ด**](https://discord.com/developers/applications) และ เปิดใช้งาน **Privileged Gateway Intents** [`MESSAGE CONTENT INTENT`](https://i.imgur.com/TiiaYR9.png) จากนั้นเชิญบอทไปที่เซิร์ฟเวอร์ด้วย scope [`applications.commands`](https://cdn.discordapp.com/attachments/939097458288496682/950613059150417970/IMG_3279.png)
* [**สร้างบอทดิสคอร์ด**](https://discord.com/developers/applications) และ เปิดใช้งาน **Privileged Gateway
Intents** [`MESSAGE CONTENT INTENT`](https://i.imgur.com/TiiaYR9.png) จากนั้นเชิญบอทไปที่เซิร์ฟเวอร์ด้วย
scope [`applications.commands`](https://cdn.discordapp.com/attachments/939097458288496682/950613059150417970/IMG_3279.png)

* [ดาวน์โหลดโปรเจคนี้](https://github.com/staciax/ValorantStoreChecker-discord-bot/archive/refs/heads/master.zip) และ รันสคริปตามข้างล่าง
* [ดาวน์โหลดโปรเจคนี้](https://github.com/staciax/ValorantStoreChecker-discord-bot/archive/refs/heads/master.zip) และ
รันสคริปตามข้างล่าง

```
pip install -r requirements.txt
Expand All @@ -89,15 +94,20 @@ pip install requests
pip install python-dotenv
```

* เก็บ Token และ Owner ID ในไฟล์ [.env](https://github.com/staciax/ValorantStoreChecker-discord-bot/blob/master/.env) และ กดบันทึกไฟล์ให้เรียบร้อย
* เก็บ Token และ Owner ID ในไฟล์ [.env](https://github.com/staciax/ValorantStoreChecker-discord-bot/blob/master/.env)
และ กดบันทึกไฟล์ให้เรียบร้อย

```
TOKEN='ใส่โทเคนของบอท'
OWNER_ID='ใส่เลขไอดีของเรา'
```

* รันสคริปให้บอททำงาน

```
python bot.py
```

* คำสั่งทับ `/` จะใช้ได้ทุกเซิฟก็ต่อเมื่อรันบอทครบ 1 ชั่วโมง
* ถ้าหากต้องการใช้งานทันทีให้พิมพ์คำสั่ง `-sync guild` ในดิสคอร์ด
* หากต้องการลบคำสั่งให้พิมพ์คำสั่ง `-unsync guild` ในดิสคอร์ด (หรือกรณีคำสั่งซ้ำกัน 2 คำสั่ง)
Expand Down Expand Up @@ -125,6 +135,7 @@ python bot.py
| `/debug` | คำสั่งแก้บัคต่างๆ เช่น `อีโมจิ`, `ราคาสกิน`,`แคช` ไม่โหลด |

<!-- ## Translations (credit by [giorgio](https://github.com/giorgi-o) -->

## แปลภาษา (บอทรองรับภษาไทยแล้ว)

ถ้าต้องการให้บอทรองรับภาษาอื่นทำตามขั้นตอนนี้
Expand All @@ -142,16 +153,17 @@ python bot.py
## Special Thanks

- [Valorant Client API](https://github.com/RumbleMike/ValorantClientAPI) by [RumbleMike](https://github.com/RumbleMike)
for providing a great API about Valorant!
for providing a great API about Valorant!

- [Valorant-API.com](https://valorant-api.com/)
for every skin names and images!
for every skin names and images!

- [Giorgio](https://github.com/giorgi-o)
for always helping me and more! <3
for always helping me and more! <3

- [Discord - Valorant App Developer ](https://discord.gg/a9yzrw3KAm) by [MikeValorantLeaks](https://github.com/RumbleMike)
developer community for valorant api
- [Discord - Valorant App Developer ](https://discord.gg/a9yzrw3KAm)
by [MikeValorantLeaks](https://github.com/RumbleMike)
developer community for valorant api

- [Contributors](https://github.com/staciax/ValorantStoreChecker-discord-bot/graphs/contributors) <3 <3

Expand All @@ -161,4 +173,4 @@ developer community for valorant api

<a href="https://tipme.in.th/renlyx">
<img link="https://ko-fi.com/staciax" src="https://static.tipme.in.th/img/logo.f8267020b29b.svg" width="170" />
</a>
</a>
14 changes: 9 additions & 5 deletions app.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"name": "discord-valorantbot",
"description": "Heroku-discord bot for valorant",
"repository": "https://github.com/staciax/Valorant-DiscordBot",
"keywords": ["python", "discord", "bot"]
}
"name": "discord-valorantbot",
"description": "Heroku-discord bot for valorant",
"repository": "https://github.com/staciax/Valorant-DiscordBot",
"keywords": [
"python",
"discord",
"bot"
]
}
58 changes: 35 additions & 23 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from __future__ import annotations

# Standard
import asyncio
import os
import sys
import traceback
import asyncio

import aiohttp
import discord
from dotenv import load_dotenv
from discord.ext import commands
from discord.ext.commands import ExtensionFailed, ExtensionNotFound, NoEntryPointError
from dotenv import load_dotenv

from utils import locale_v2
from utils.valorant.cache import get_cache

Expand All @@ -19,44 +21,46 @@
'cogs.errors',
'cogs.notify',
'cogs.valorant'
]
]

# intents required
intents = discord.Intents.default()
intents.message_content = True

BOT_PREFIX = '-'


class ValorantBot(commands.Bot):
debug: bool
bot_app_info: discord.AppInfo

def __init__(self) -> None:
super().__init__(command_prefix=BOT_PREFIX, case_insensitive=True, intents=intents)
self.bot_version = '3.1.0-aiohttp'
self.bot_version = '3.1.2'
self.tree.interaction_check = self.interaction_check

async def interaction_check(self, interaction: discord.Interaction) -> bool:
locale_v2.set_interaction_locale(interaction.locale) # bot responses localized
locale_v2.set_valorant_locale(interaction.locale) # valorant localized
return True


@staticmethod
async def interaction_check(interaction: discord.Interaction) -> bool:
locale_v2.set_interaction_locale(interaction.locale) # bot responses localized # wait for update
locale_v2.set_valorant_locale(interaction.locale) # valorant localized
return True

@property
def owner(self) -> discord.User:
return self.bot_app_info.owner

async def on_ready(self) -> None:
async def on_ready(self) -> None:
await self.tree.sync()
print(f"\nLogged in as: {self.user}\n\n BOT IS READY !")
print(f"Version: {self.bot_version}")

# bot presence
activity_type = discord.ActivityType.listening
await self.change_presence(activity=discord.Activity(type=activity_type, name="(╯•﹏•╰)"))

async def setup_hook(self) -> None:
self.session = aiohttp.ClientSession()

try:
self.owner_id = int(os.getenv('OWNER_ID'))
except ValueError:
Expand All @@ -70,26 +74,34 @@ async def load_cogs(self) -> None:
for ext in initial_extensions:
try:
await self.load_extension(ext)
except Exception as e:
except (
ExtensionNotFound,
NoEntryPointError,
ExtensionFailed,
):
print(f'Failed to load extension {ext}.', file=sys.stderr)
traceback.print_exc()

def setup_cache(self) -> None:
@staticmethod
def setup_cache() -> None:
try:
open('data/cache.json')
except FileNotFoundError:
get_cache()

async def close(self) -> None:
await self.session.close()

async def start(self, debug:bool = False) -> None:
await super().close()

async def start(self, debug: bool = False) -> None:
self.debug = debug
return await super().start(os.getenv('TOKEN'), reconnect=True)


def run_bot() -> None:
bot = ValorantBot()
asyncio.run(bot.start(debug=False))


if __name__ == '__main__':
run_bot()
run_bot()
Loading

0 comments on commit 8ddd586

Please sign in to comment.