File size: 1,530 Bytes
1d000a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import discord
import os
import asyncio
from discord.ext import commands

# Replace with your bot token
TOKEN = "MTI0NDI2NTU5MDc1MjAyMjU4Mg.G-KWFf.2mjabK1nmRsXRRBrbqxrWGEIXkGt_CLzFvP_CA"
# Replace with your channel ID
CHANNEL_ID = 1238313627266711573

# Initial address for the image folder
IMAGE_FOLDER = r"C:\Users\shash\Downloads\Automation\Hf_Demo\Demo_Visual"

# Define the intents
intents = discord.Intents.default()
intents.message_content = True

# Bot command prefix
bot = commands.Bot(command_prefix='!', intents=intents)

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user.name}')
    print('------')

@bot.command()
async def upload_image(ctx, *,image_name):
    file_path = os.path.join(IMAGE_FOLDER, image_name)
    if not os.path.isfile(file_path):
        await ctx.send(f'File {image_name} not found.')
        return

    if not image_name.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
        await ctx.send(f'File {image_name} is not a valid image format.')
        return

    try:
        await ctx.send(file=discord.File(file_path))
        print(f'Uploaded {image_name}')
    except Exception as e:
        print(f'Error uploading image: {e}')

# Run the bot within an event loop
async def run_bot():
    try:
        await bot.start(TOKEN)
    except Exception as e:
        print(f'Error starting bot: {e}')

# Create an event loop and run the bot
loop = asyncio.get_event_loop()
loop.create_task(run_bot())
loop.run_forever()