Skip to content

Commit

Permalink
Merge pull request #20 from Yuuzi261/develope
Browse files Browse the repository at this point in the history
0.3.5
  • Loading branch information
Yuuzi261 authored Nov 12, 2023
2 parents 7901012 + 7dc1e7e commit cc76fd1
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 25 deletions.
61 changes: 59 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

A Twitter Alert Bot For Discord

[**English**](./README.md) | [**中文繁體**](./README_zh.md)
[**English**](./README.md) | [**繁體中文**](./README_zh.md)

</div>

Expand Down Expand Up @@ -50,6 +50,10 @@ Tweetcord is a discord bot that uses the tweety-ns module to let you receive twe
| `username` | str | The username of the twitter user you want to turn off notifications for |
| `channel` | discord.TextChannel | The channel which set to delivers notifications |

👉 `/list users`

- List all twitter users whose notifications are enabled on the current server

</details>

## 📥Installation
Expand All @@ -64,7 +68,60 @@ In certain operating systems, you may need to use the command `pip3` instead of

## ⚡Usage

**📢This tutorial is suitable for version 0.3.2 or later. (Recommended: 0.3.4 or later)**
**📢This tutorial is suitable for version 0.3.2 or later. (Recommended: 0.3.5 or later)**

<details>
<summary><b>📌click here to upgrade from 0.3.4 to 0.3.5</b></summary>

Create a python file in the `cogs` folder and name it `upgrade.py`. Paste the following code and run the bot. Use the slash command `/upgrade` to upgrade. This cog can be removed after the upgrade is completed.

```py
import discord
from discord import app_commands
from core.classes import Cog_Extension
import sqlite3
import os

from src.log import setup_logger
from src.permission_check import is_administrator

log = setup_logger(__name__)

class Upgrade(Cog_Extension):

@is_administrator()
@app_commands.command(name='upgrade', description='upgrade to Tweetcord 0.3.5')
async def upgrade(self, itn: discord.Interaction):

await itn.response.defer(ephemeral=True)

conn = sqlite3.connect(f"{os.getenv('DATA_PATH')}tracked_accounts.db")
cursor = conn.cursor()

cursor.executescript('ALTER TABLE channel ADD server_id TEXT')

cursor.execute('SELECT id FROM channel')
channels = cursor.fetchall()

for c in channels:
try:
channel = self.bot.get_channel(int(c[0]))
cursor.execute('UPDATE channel SET server_id = ? WHERE id = ?', (channel.guild.id, channel.id))
except:
log.warning(f'the bot cannot obtain channel: {c[0]}, but this will not cause problems with the original features. The new feature can also be used normally on existing servers.')


conn.commit()
conn.close()

await itn.followup.send('successfully upgrade to 0.3.5, you can remove this cog.')


async def setup(bot):
await bot.add_cog(Upgrade(bot))
```

</details>

<details>
<summary><b>📌click here to upgrade from 0.3.3 to 0.3.4</b></summary>
Expand Down
67 changes: 65 additions & 2 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Discord的Twitter通知機器人

[**English**](./README.md) | [**中文繁體**](./README_zh.md)
[**English**](./README.md) | [**繁體中文**](./README_zh.md)

</div>

Expand Down Expand Up @@ -50,6 +50,10 @@ Tweetcord是一個discord機器人,它使用tweety-ns模組讓你在discord上
| `username` | str | 你想要關閉通知的Twitter用戶的用戶名 |
| `channel` | discord.TextChannel | 設置為發送通知的頻道 |

👉 `/list users`

- 列出所有當前伺服器開啟通知的Twitter用戶

</details>

## 📥安裝
Expand All @@ -64,7 +68,60 @@ pip install -r requirements.txt

## ⚡使用

**📢本教學適用於0.3.2或更高版本。(建議:0.3.4或更高版本)**
**📢本教學適用於0.3.2或更高版本。(建議:0.3.5或更高版本)**

<details>
<summary><b>📌0.3.4升級到0.3.5請點這裡</b></summary>

`cogs` 資料夾創建一個python檔案並命名為 `upgrade.py`,貼上下面的程式碼並運行機器人,使用斜線指令 `/upgrade` 進行升級。升級結束後可以移除這個cog。

```py
import discord
from discord import app_commands
from core.classes import Cog_Extension
import sqlite3
import os

from src.log import setup_logger
from src.permission_check import is_administrator

log = setup_logger(__name__)

class Upgrade(Cog_Extension):

@is_administrator()
@app_commands.command(name='upgrade', description='upgrade to Tweetcord 0.3.5')
async def upgrade(self, itn: discord.Interaction):

await itn.response.defer(ephemeral=True)

conn = sqlite3.connect(f"{os.getenv('DATA_PATH')}tracked_accounts.db")
cursor = conn.cursor()

cursor.executescript('ALTER TABLE channel ADD server_id TEXT')

cursor.execute('SELECT id FROM channel')
channels = cursor.fetchall()

for c in channels:
try:
channel = self.bot.get_channel(int(c[0]))
cursor.execute('UPDATE channel SET server_id = ? WHERE id = ?', (channel.guild.id, channel.id))
except:
log.warning(f'the bot cannot obtain channel: {c[0]}, but this will not cause problems with the original features. The new feature can also be used normally on existing servers.')


conn.commit()
conn.close()

await itn.followup.send('successfully upgrade to 0.3.5, you can remove this cog.')


async def setup(bot):
await bot.add_cog(Upgrade(bot))
```

</details>

<details>
<summary><b>📌0.3.3升級到0.3.4請點這裡</b></summary>
Expand Down Expand Up @@ -132,3 +189,9 @@ python bot.py
### 4. 玩得開心

現在你可以回到Discord,並使用 `/add notifier` 指令來設置你想要接收更新的Twitter用戶!

## 💪貢獻者

感謝所有貢獻者。

[![](https://contrib.rocks/image?repo=Yuuzi261/Tweetcord)](https://github.com/Yuuzi261/Tweetcord/graphs/contributors)
25 changes: 10 additions & 15 deletions cogs/ListUsers.py → cogs/list_users.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import discord
from discord import app_commands
from discord.ext import commands
from core.classes import Cog_Extension
import sqlite3
import os

from src.permission_check import is_administrator

class ListUsersCog(commands.Cog):

def __init__(self, bot):
self.bot = bot
class ListUsers(Cog_Extension):

list_group = app_commands.Group(name='list', description="List something")

@is_administrator()
@app_commands.command(
name='list_users',
description='Lists registered Twitter usernames and their associated channels'
)
@list_group.command(name='users')
async def list_users(self, itn: discord.Interaction):
"""Lists all exists notifier on your server."""

server_id = itn.guild_id

Expand All @@ -31,14 +28,14 @@ async def list_users(self, itn: discord.Interaction):
ON user.id = notification.user_id
JOIN channel
ON notification.channel_id = channel.id
WHERE notification.server_id = ? AND notification.enabled = 1
WHERE channel.server_id = ? AND notification.enabled = 1
""", (str(server_id),))
user_channel_role_data = cursor.fetchall()

conn.close()

formatted_data = [
f"{i+1}. ```{username}``` <#{channel_id}> <@&{role_id}>" if role_id else f"{i+1}. {username} <#{channel_id}>"
f"{i+1}. ```{username}``` <#{channel_id}> <@&{role_id}>" if role_id else f"{i+1}. ```{username}``` <#{channel_id}>"
for i, (username, channel_id, role_id) in enumerate(user_channel_role_data)
]

Expand All @@ -53,10 +50,8 @@ async def list_users(self, itn: discord.Interaction):
color=0x778899
)

await itn.response.send_message(embed=embed)


await itn.response.send_message(embed=embed, ephemeral=True)


async def setup(bot):
await bot.add_cog(ListUsersCog(bot))
await bot.add_cog(ListUsers(bot))
8 changes: 4 additions & 4 deletions cogs/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ async def notifier(self, itn : discord.Interaction, username: str, channel: disc
return

cursor.execute('INSERT INTO user VALUES (?, ?, ?)', (str(new_user.id), username, datetime.utcnow().replace(tzinfo=timezone.utc).strftime('%Y-%m-%d %H:%M:%S%z')))
cursor.execute('INSERT OR IGNORE INTO channel VALUES (?)', (str(channel.id),))
cursor.execute('INSERT INTO notification (user_id, channel_id, role_id, server_id) VALUES (?, ?, ?, ?)', (str(new_user.id), str(channel.id), roleID, server_id))
cursor.execute('INSERT OR IGNORE INTO channel VALUES (?, ?)', (str(channel.id), server_id))
cursor.execute('INSERT INTO notification (user_id, channel_id, role_id) VALUES (?, ?, ?)', (str(new_user.id), str(channel.id), roleID))

app.follow_user(new_user)

if app.enable_user_notification(new_user): log.info(f'successfully opened notification for {username}')
else: log.warning(f'unable to turn on notifications for {username}')
else:
cursor.execute('INSERT OR IGNORE INTO channel VALUES (?)', (str(channel.id),))
cursor.execute('REPLACE INTO notification (user_id, channel_id, role_id, server_id) VALUES (?, ?, ?, ?)', (match_user['id'], str(channel.id), roleID, server_id))
cursor.execute('INSERT OR IGNORE INTO channel VALUES (?, ?)', (str(channel.id), server_id))
cursor.execute('REPLACE INTO notification (user_id, channel_id, role_id) VALUES (?, ?, ?)', (match_user['id'], str(channel.id), roleID))

conn.commit()
conn.close()
Expand Down
4 changes: 2 additions & 2 deletions src/db_function/init_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ def init_db():
cursor = conn.cursor()
cursor.executescript("""
CREATE TABLE IF NOT EXISTS user (id TEXT PRIMARY KEY, username TEXT, lastest_tweet TEXT);
CREATE TABLE IF NOT EXISTS channel (id TEXT PRIMARY KEY);
CREATE TABLE IF NOT EXISTS notification (user_id TEXT, channel_id TEXT, role_id TEXT, server_id TEXT, enabled INTEGER DEFAULT 1, FOREIGN KEY (user_id) REFERENCES user (id), FOREIGN KEY (channel_id) REFERENCES channel (id), PRIMARY KEY(user_id, channel_id));
CREATE TABLE IF NOT EXISTS channel (id TEXT PRIMARY KEY, server_id TEXT);
CREATE TABLE IF NOT EXISTS notification (user_id TEXT, channel_id TEXT, role_id TEXT, enabled INTEGER DEFAULT 1, FOREIGN KEY (user_id) REFERENCES user (id), FOREIGN KEY (channel_id) REFERENCES channel (id), PRIMARY KEY(user_id, channel_id));
""")
conn.commit()
conn.close()

0 comments on commit cc76fd1

Please sign in to comment.