Skip to content

Commit a01d9f4

Browse files
committed
initial commit
0 parents  commit a01d9f4

File tree

6 files changed

+106
-0
lines changed

6 files changed

+106
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

README.md

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p align=center>
2+
<img src="logo.png" width="250" />
3+
</p>
4+
5+
A little discord bot for forwarding messages from one channel to another.
6+
7+
Add it! https://discord.com/oauth2/authorize?client_id=1279742984912375819
8+
9+
## Installation / Selfhosting
10+
11+
1. Clone the repository
12+
13+
```bash
14+
git clone https://github.com/honey-team/ForwardBot.git
15+
```
16+
17+
2. Install the dependencies
18+
19+
```bash
20+
pip install -r requirements.txt
21+
```
22+
23+
3. Create a `.env` file in the root directory and add the following variables:
24+
25+
```bash
26+
TOKEN=your-token-here
27+
EMOJI=your-emoji-here
28+
```
29+
30+
Create a new application on the [Discord Developer Portal](https://discord.com/developers/applications) and copy the token, then add forward.png as emoji to the application. (bot is intended to support user install)
31+
32+
4. Run the bot
33+
34+
```bash
35+
python main.py
36+
```

forward.png

15.5 KB
Loading

logo.png

34.3 KB
Loading

main.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import discord
2+
from discord import app_commands
3+
from discord.ext import commands
4+
from dotenv import load_dotenv
5+
from os import getenv
6+
load_dotenv()
7+
8+
bot = discord.Client(intents=discord.Intents.default())
9+
tree = app_commands.CommandTree(bot)
10+
messages: dict[int, list[discord.Message]] = {}
11+
12+
@bot.event
13+
async def on_ready():
14+
await tree.sync()
15+
16+
@tree.context_menu(name='Forward')
17+
@app_commands.user_install()
18+
async def forward(interaction: discord.Interaction, message: discord.Message):
19+
await interaction.response.defer(ephemeral=True)
20+
if interaction.user.id not in messages:
21+
messages[interaction.user.id] = [message]
22+
await interaction.followup.send('Message saved. Now, use the `/send` command to send it to another channel.')
23+
return
24+
class AskButton(discord.ui.Button):
25+
async def callback(self, ctx: discord.Interaction):
26+
if self.custom_id == 'yes':
27+
if len(messages[ctx.user.id]) < 10:
28+
messages[ctx.user.id].append(message)
29+
await ctx.response.edit_message(content='Message added to the list. Now, use the `/send` command to send all messages to another channel.',view=None)
30+
else:
31+
await ctx.response.send_message(content='You have reached the maximum limit of 10 messages. Please send the messages you have saved using the `/send` command or overwrite.')
32+
return
33+
messages[ctx.user.id] = [message]
34+
await ctx.response.edit_message(content='Message saved. Now, use the `/send` command to send it to another channel.', view=None)
35+
view = discord.ui.View()
36+
view.add_item(AskButton(label='Yes', custom_id='yes', style=discord.ButtonStyle.green))
37+
view.add_item(AskButton(label='No, delete all other messages', custom_id='no', style=discord.ButtonStyle.red))
38+
if len(message.attachments) > 1 and discord.utils.find(lambda a: a.content_type.startswith('image'), message.attachments):
39+
await interaction.followup.send('You have already saved a message. Would you like to add it to the list?\n-# NOTE: You can save up to 10 messages. Only first image will be visible as image.', view=view)
40+
return
41+
await interaction.followup.send('You have already saved a message. Would you like to add it to the list?\n-# NOTE: You can save up to 10 messages.', view=view)
42+
43+
@tree.command(description='Send the saved message(s) to another channel')
44+
@app_commands.user_install()
45+
async def send(ctx: discord.Interaction):
46+
if ctx.user.id not in messages:
47+
await ctx.response.send_message('You have not saved any messages to send. Use context menu option `Forward`', ephemeral=True)
48+
return
49+
embeds: list[discord.Embed] = []
50+
for i, message in enumerate(messages[ctx.user.id]):
51+
if i == 0:
52+
embeds.append(discord.Embed(title=f'{getenv('EMOJI') or ""} *Forwarded*', description=message.content, timestamp=message.created_at))
53+
else:
54+
embeds.append(discord.Embed(description=message.content, timestamp=message.created_at))
55+
embeds[i].set_author(name=message.author.name, icon_url=message.author.display_avatar.url)
56+
image = discord.utils.find(lambda a: a.content_type.startswith('image'), message.attachments)
57+
if image is not None:
58+
embeds[i].set_image(url=image.url)
59+
for a in message.attachments:
60+
if a != image:
61+
embeds[i].add_field(name=a.filename, value=f'[Link to attachment]({a.url})')
62+
embeds[i].add_field(name='Original Message', value=f'[Link to message]({message.jump_url})', inline=False)
63+
embeds[i].set_footer(text=f'Forwarded by {ctx.user.name}', icon_url=ctx.user.display_avatar.url)
64+
await ctx.response.send_message(embeds=embeds)
65+
del messages[ctx.user.id]
66+
67+
bot.run(getenv('TOKEN'))

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
python-dotenv
2+
discord.py>=2.4

0 commit comments

Comments
 (0)