|
import discord |
|
from discord.ext import commands |
|
from discord import app_commands |
|
import os |
|
import random |
|
import json |
|
|
|
intents = discord.Intents.all() |
|
bot = commands.Bot(command_prefix='/', intents=intents) |
|
|
|
|
|
TRIPLES_WELCOME_FOLDER_PATH = '/path/to/triples/welcome' |
|
ARTMS_WELCOME_FOLDER_PATH = '/path/to/artms/welcome' |
|
|
|
|
|
user_image_mapping = {} |
|
|
|
|
|
USER_DATA_FILE = 'user_image_mapping.json' |
|
|
|
|
|
if os.path.exists(USER_DATA_FILE): |
|
with open(USER_DATA_FILE, 'r') as f: |
|
user_image_mapping = json.load(f) |
|
|
|
@bot.event |
|
async def on_ready(): |
|
print(f'Logged in as {bot.user} (ID: {bot.user.id})') |
|
|
|
async def artist_autocompletion( |
|
interaction: discord.Interaction, |
|
current: str |
|
) -> typing.List[app_commands.Choice[str]]: |
|
"""Artist 자동 완성을 위한 함수""" |
|
artists = ['tripleS', 'ARTMS'] |
|
return [ |
|
app_commands.Choice(name=art, value=art) |
|
for art in artists if current.lower() in art.lower() |
|
] |
|
|
|
@bot.tree.command(name="follow") |
|
@app_commands.autocomplete(artist=artist_autocompletion) |
|
async def follow(interaction: discord.Interaction, artist: str): |
|
"""지정된 artist의 이미지를 가져옵니다.""" |
|
user_id = str(interaction.user.id) |
|
|
|
if artist.lower() == "triples": |
|
folder_path = TRIPLES_WELCOME_FOLDER_PATH |
|
text_first_time = "tripleS 이미지입니다!" |
|
text_repeat = "다시 오신 것을 환영합니다, tripleS 팬님!" |
|
elif artist.lower() == "artms": |
|
folder_path = ARTMS_WELCOME_FOLDER_PATH |
|
text_first_time = "ARTMS 이미지입니다!" |
|
text_repeat = "다시 오신 것을 환영합니다, ARTMS 팬님!" |
|
else: |
|
await interaction.response.send_message("올바른 Artist를 선택하세요.", ephemeral=False) |
|
return |
|
|
|
if user_id not in user_image_mapping: |
|
if not os.path.exists(folder_path) or not os.listdir(folder_path): |
|
await interaction.response.send_message("이미지가 준비되지 않았습니다.", ephemeral=False) |
|
return |
|
|
|
random_image = random.choice(os.listdir(folder_path)) |
|
user_image_mapping[user_id] = os.path.join(folder_path, random_image) |
|
text = text_first_time |
|
|
|
|
|
with open(USER_DATA_FILE, 'w') as f: |
|
json.dump(user_image_mapping, f) |
|
else: |
|
random_image = user_image_mapping[user_id] |
|
text = text_repeat |
|
|
|
with open(random_image, "rb") as f: |
|
image_data = discord.File(f) |
|
|
|
await interaction.response.send_message(content=text, file=image_data) |
|
|
|
if __name__ == "__main__": |
|
token = os.environ['DISCORD_TOKEN'] |
|
bot.run(token) |