Skip to content
This repository was archived by the owner on Mar 14, 2021. It is now read-only.
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
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ name = "pypi"
aiodns = "*"
aiohttp = "<2.3.0,>=2.0.0"
websockets = ">=4.0,<5.0"
"discord.py" = {git = "https://github.com/Rapptz/discord.py", ref = "rewrite", extras = ["voice"]}

[dev-packages]
"flake8" = "*"
Expand Down
15 changes: 11 additions & 4 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 47 additions & 2 deletions bot/cogs/snakes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# coding=utf-8
import asyncio
import json
import logging
from typing import Any, Dict
import random
from typing import Tuple

from discord import Embed, FFmpegPCMAudio
from discord.ext.commands import AutoShardedBot, Context, command
from discord.opus import is_loaded, load_opus

log = logging.getLogger(__name__)

Expand All @@ -14,8 +19,14 @@ class Snakes:

def __init__(self, bot: AutoShardedBot):
self.bot = bot
self.python_image = "https://www.python.org/static/community_logos/python-logo-master-v3-TM.png"
self.python_info = ("The Python Programming language(Python Sermone) is a dynamically typed, interpreted "
"programming language. It is a member of the high level programming languges usually "
"found in areas like backend web development data science and AI.")
with open("bot/db/db.json", "r") as db:
self.db = json.load(db)

async def get_snek(self, name: str = None) -> Dict[str, Any]:
async def get_snek(self, name: str = None) -> Tuple[str, str, str]:
"""
Go online and fetch information about a snake

Expand All @@ -28,6 +39,17 @@ async def get_snek(self, name: str = None) -> Dict[str, Any]:
:param name: Optional, the name of the snake to get information for - omit for a random snake
:return: A dict containing information on a snake
"""
if name:
name = name.lower()
if name == "python":
return ("python", self.python_info, self.python_image)
else:
for key in self.db.keys():
if key.lower() == name:
return (key, self.db[key][0], self.db[key][1])
else:
key = random.choice(list(self.db))
return (key, self.db[key][0], self.db[key][1])

@command()
async def get(self, ctx: Context, name: str = None):
Expand All @@ -40,10 +62,33 @@ async def get(self, ctx: Context, name: str = None):
:param ctx: Context object passed from discord.py
:param name: Optional, the name of the snake to get information for - omit for a random snake
"""
snake = await self.get_snek(name)
if snake:
snake_embed = Embed(title=snake[0], description=snake[1])
snake_embed.set_image(url=snake[2])
await ctx.send(embed=snake_embed)
else:
await ctx.send("I was not able to find your snake, I am sorry.")

# Any additional commands can be placed here. Be creative, but keep it to a reasonable amount!
@command()
async def hiss(self, ctx: Context):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone executes this twice within a short period of time will there be an error, or will the sound play twice at the same time?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to check the clients voice status before making it play more.

Copy link
Author

@hargoniX hargoniX Mar 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discordpy appears to handle this fine.(just tested it)

"""
Hisssses in a voice channel
"""
voice_channel = ctx.message.author.voice.channel
if voice_channel:
voice_client = await voice_channel.connect()
hiss = FFmpegPCMAudio("bot/db/sound1.mp3")

def disconnect(errors):
disconnect_coro = voice_client.disconnect()
asyncio.run_coroutine_threadsafe(disconnect_coro, self.bot.loop)
voice_client.play(hiss, after=disconnect)


def setup(bot):
if not is_loaded():
load_opus()
bot.add_cog(Snakes(bot))
log.info("Cog loaded: Snakes")
1 change: 1 addition & 0 deletions bot/db/db.json

Large diffs are not rendered by default.

Binary file added bot/db/sound1.mp3
Binary file not shown.