Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 51 additions & 19 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import os
import os, traceback
import nextcord
import psutil
from nextcord.ext import commands, tasks
from dotenv import load_dotenv
import certifi, ssl

ssl_context = ssl.create_default_context(cafile=certifi.where())

# 환경 변수에서 토큰 로드
load_dotenv("token.env")
token = os.getenv("token")

intents = nextcord.Intents.default()
intents.message_content = True

last_status = None

bot = commands.Bot(command_prefix="!", intents=intents)

@bot.event
Expand All @@ -19,40 +23,68 @@ async def on_ready():
await bot.change_presence(
activity=nextcord.Game(name="기본 상태 | 준비 완료 🎉")
)
update_server_status.start() # 서버 상태 실시간 업데이트 시작
update_server_status.start()

@bot.event
async def on_message(message):
if message.author == bot.user:
return

if "이벤트 시작" in message.content:
await bot.change_presence(
activity=nextcord.Activity(type=nextcord.ActivityType.playing, name="유저 이벤트 관리 중 🛠️")
)
await message.channel.send("상태가 '유저 이벤트 관리 중 🛠️'로 변경되었습니다!")
elif "게임 시작" in message.content:
if not message.author.guild_permissions.administrator:
return

content = message.content.strip()

match content:
case "이벤트 시작":
status = "유저 이벤트 관리 중 🛠️"
case "게임 시작":
status = "사용자와 함께 게임 중 🎮"
case _:
return

if bot.activity and bot.activity.name == status:
return

try:
await bot.change_presence(
activity=nextcord.Game(name="사용자와 함께 게임 중 🎮")
activity=nextcord.Game(name=status)
)
await message.channel.send("상태가 '사용자와 함께 게임 중 🎮'로 변경되었습니다!")
await message.channel.send(f"상태가 '{status}'로 변경되었습니다!")
except nextcord.HTTPException:
await message.channel.send("상태 변경 중 오류가 발생했습니다.")

await bot.process_commands(message)

@tasks.loop(seconds=10)
@tasks.loop(seconds=60)
async def update_server_status():
cpu_usage = psutil.cpu_percent()
ram_usage = psutil.virtual_memory().percent
status_message = f"CPU: {cpu_usage}% | RAM: {ram_usage}%"
global last_status

cpu = psutil.cpu_percent()
ram = psutil.virtual_memory().percent
status = f"CPU: {cpu}% | RAM: {ram}%"

if status == last_status:
return

last_status = status

await bot.change_presence(
activity=nextcord.Activity(type=nextcord.ActivityType.watching, name=status_message)
activity=nextcord.Activity(type=nextcord.ActivityType.watching, name=status)
)

for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
bot.load_extension(f'cogs.{filename[:-3]}')
print(f'로드됨: {filename[:-3]}')
if not filename.endswith('.py') or filename.startswith('._'):
continue

ext = f'cogs.{filename[:-3]}'

try:
bot.load_extension(ext)
print(f'로드됨: {ext}')
except Exception as e:
print(f'로드 실패: {ext}')
traceback.print_exception(type(e), e, e.__traceback__)

@update_server_status.before_loop
async def before_update_server_status():
Expand Down
58 changes: 14 additions & 44 deletions cogs/가위바위보.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import random
import nextcord
from nextcord.ext import commands

Expand All @@ -11,6 +10,13 @@ def __init__(self, bot):
"보": "🖐️"
}

# 봇이 이기는 경우만 정의
self.win_map = {
"가위": "바위",
"바위": "보",
"보": "가위"
}

@nextcord.slash_command(name="가위바위보", description="가위바위보 게임을 즐겨보세요!")
async def play_rps(
self,
Expand All @@ -21,50 +27,14 @@ async def play_rps(
choices=["가위", "바위", "보"]
),
):
probabilities = {
"win": 0.5,
"draw": 0.3,
"lose": 0.2
}
outcome = random.choices(
population=["win", "draw", "lose"],
weights=[probabilities["win"], probabilities["draw"], probabilities["lose"]],
k=1
)[0]
bot_choice = self.calculate_bot_choice(원하는것, outcome)
bot_choice_with_emoji = f"{bot_choice} {self.choices[bot_choice]}"
result = self.get_result_message(outcome)
await interaction.response.send_message(
f"**당신의 선택:** {원하는것} {self.choices[원하는것]}\n"
f"**봇의 선택:** {bot_choice_with_emoji}\n\n"
f"**결과:** {result}"
)

def calculate_bot_choice(self, 원하는것, outcome):
if outcome == "win":
if 원하는것 == "가위":
return "보"
elif 원하는것 == "바위":
return "가위"
elif 원하는것 == "보":
return "바위"
elif outcome == "draw":
return 원하는것
elif outcome == "lose":
if 원하는것 == "가위":
return "바위"
elif 원하는것 == "바위":
return "보"
elif 원하는것== "보":
return "가위"
bot_choice = self.win_map[원하는것]

def get_result_message(self, outcome):
if outcome == "win":
return "축하합니다! 당신이 이겼습니다! 🎉"
elif outcome == "draw":
return "무승부입니다!"
elif outcome == "lose":
return "아쉽지만, 봇이 이겼습니다! 😄"
await interaction.response.send_message(
f"당신의 선택: {원하는것} {self.choices[원하는것]}\n"
f"봇의 선택: {bot_choice} {self.choices[bot_choice]}\n\n"
f"결과: 봇이 이겼습니다. 정말 못하시네요. 사람 맞나요?"
)

def setup(bot):
bot.add_cog(가위바위보(bot))
bot.add_cog(가위바위보(bot))
Loading