-
Notifications
You must be signed in to change notification settings - Fork 0
/
cowsay-bot.py
executable file
·64 lines (51 loc) · 1.79 KB
/
cowsay-bot.py
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
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
import datetime
from dotenv import dotenv_values
from os import getenv
import cowsay
import discord
dotenv = dotenv_values(".env")
TOKEN = getenv("TOKEN") or dotenv["TOKEN"]
if not TOKEN:
raise Exception("TOKEN environment variable must be defined")
GUILD_IDS = getenv("GUILD_IDS") or dotenv["GUILD_IDS"]
if not GUILD_IDS:
raise Exception("GUILD_IDS environment variable must be defined")
GUILD_IDS = [int(guild_id) for guild_id in GUILD_IDS.split(",")]
GUILDS = [discord.Object(guild_id) for guild_id in GUILD_IDS]
intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = discord.app_commands.CommandTree(client)
@tree.command(
name="cowsay",
description="Make a character say something.",
guilds=GUILDS,
)
async def _cowsay(
interaction: discord.Interaction,
character: str,
text: str,
):
print(f"Sending a {character} at {datetime.datetime.now()}")
character = character.lower()
if character in cowsay.CHARS:
await interaction.response.send_message(f"```\n{cowsay.get_output_string(character, text)}\n```")
else:
say = cowsay.get_output_string("cow", f"{character} does not exist as a character. Try the following: {', '.join(cowsay.char_names)}.")
await interaction.response.send_message(f"```\n{say}\n```")
@_cowsay.autocomplete("character")
async def character_autocomplete(
interaction: discord.Interaction,
current: str,
):
current = current.lower()
return [
discord.app_commands.Choice(name=char, value=char)
for char in cowsay.char_names if current in char
][:25]
@client.event
async def on_ready():
for guild in GUILDS:
await tree.sync(guild=guild)
cowsay.cow(f"I'm ready! GUILD_IDS={GUILD_IDS}")
client.run(TOKEN)