Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
➕Added blacklisting users. (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmattbtw authored Aug 1, 2021
1 parent 1bce2a9 commit b5240a3
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
config.json
venv
.cache
blacklist.json
blacklist.json
blacklist_user.json
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@
* You can also use a Spotify URL or URI
* You can get a URI by searching a song on Spotify, then holding down the `Ctrl` key while right clicking on the song, and then selecting "Copy Spotify URI" from the Share menu.

### `💎` Other Commands:
- Variables:

* `{user}` - Twitch Username

* `{song}` - Can be a song name, a Spotify URI, or a Spotify URL

- Commands:

* `!ping` - Checks if the bot is online + shows what version

* `!blacklistuser {user}` - Blacklists a user from using the bot

* `!unblacklistuser {user}` - Unblacklists a user from using the bot

* `!blacklist {song}` - Blacklists a song from being played

* `!unblacklist {song}` - Unblacklists a song from being played

* `!np` - Shows the current song

* `!songrequest {song}` - Requests a song to be played

### `🙌` Code Contributors

<table>
Expand Down
98 changes: 68 additions & 30 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@

from twitchio.ext.commands.errors import MissingRequiredArgument

config_path = os.path.join(".", "config.json")
blacklist_path = os.path.join(".", "blacklist.json")

if not os.path.exists(config_path):
def path_exists(filename):
return os.path.join(".", f"{filename}")


if not os.path.exists(path_exists("config.json")):
print("Config file not found. Exiting.\nPlease run `setup.py`")
exit()

if not os.path.exists(blacklist_path):
if not os.path.exists(path_exists("blacklist.json")):
print(
"Blacklist file not found. Exiting.\nPlease run `setup.py`\n(or make a `blacklist.json` file yourself, if you know how to)\nhttps://github.com/mmattbtw/TwitchTunes/wiki/Blacklist.json"
)
exit()

if not os.path.exists(path_exists("blacklist_user.json")):
print(
"Blacklisted users file not found. Exiting.\nPlease run `setup.py`\n(or make a `blacklist_user.json` file yourself, if you know how to)\nhttps://github.com/mmattbtw/TwitchTunes/wiki/Blacklist.json"
)
exit()

os.system("pip install -U -r requirements.txt")
print("\n\nStarting 🎶TwitchTunes")

Expand Down Expand Up @@ -69,7 +77,7 @@ def __init__(self):
)

self.token = os.environ.get("SPOTIFY_AUTH")
self.version = "1.2.4"
self.version = "1.2.5"

async def event_ready(self):
print("\n" * 100)
Expand All @@ -90,6 +98,32 @@ async def ping_command(self, ctx):
def is_owner(self, ctx):
return ctx.author.id == "640348450"

@commands.command(name="blacklistuser")
async def blacklist_user(self, ctx, *, user: str):
if ctx.author.is_mod or self.is_owner(ctx):
file = self.read_json("blacklist_user")
if user not in file["users"]:
file["users"].append(user)
self.write_json(file, "blacklist_user")
await ctx.send(f"{user} added to blacklist")
else:
await ctx.send(f"{user} is already blacklisted")
else:
await ctx.send("You don't have permission to do that.")

@commands.command(name="unblacklistuser")
async def unblacklist_user(self, ctx, *, user: str):
if ctx.author.is_mod or self.is_owner(ctx):
file = self.read_json("blacklist_user")
if user in file["users"]:
file["users"].remove(user)
self.write_json(file, "blacklist_user")
await ctx.send(f"{user} removed from blacklist")
else:
await ctx.send(f"{user} is not blacklisted")
else:
await ctx.send("You don't have permission to do that.")

@commands.command(name="blacklist", aliases=["blacklistsong", "blacklistadd"])
async def blacklist_command(self, ctx, *, song_uri: str):
if ctx.author.is_mod or self.is_owner(ctx):
Expand Down Expand Up @@ -212,32 +246,36 @@ async def songrequest_command(self, ctx, *, song: str):
# return

async def song_request(self, ctx, song, song_uri, album: bool):
jscon = self.read_json("blacklist")

if song_uri is None:
data = sp.search(song, limit=1, type="track", market="US")
song_uri = data["tracks"]["items"][0]["uri"]

song_id = song_uri.replace("spotify:track:", "")

if not album:
data = sp.track(song_id)
song_name = data["name"]
song_artists = data["artists"]
song_artists_names = [artist["name"] for artist in song_artists]
duration = data["duration_ms"] / 60000

if song_uri != "not found":
if song_uri in jscon["blacklist"]:
await ctx.send("That song is blacklisted.")
blacklisted_users = self.read_json("blacklist_user")["users"]
if ctx.author.name in blacklisted_users:
await ctx.send("You are blacklisted from requesting songs.")
else:
jscon = self.read_json("blacklist")

elif duration > 17:
await ctx.send("Send a shorter song please! :)")
else:
sp.add_to_queue(song_uri)
await ctx.send(
f"Your song ({song_name} by {', '.join(song_artists_names)}) [ {data['external_urls']['spotify']} ] has been added to {ctx.channel.name}'s queue!"
)
if song_uri is None:
data = sp.search(song, limit=1, type="track", market="US")
song_uri = data["tracks"]["items"][0]["uri"]

song_id = song_uri.replace("spotify:track:", "")

if not album:
data = sp.track(song_id)
song_name = data["name"]
song_artists = data["artists"]
song_artists_names = [artist["name"] for artist in song_artists]
duration = data["duration_ms"] / 60000

if song_uri != "not found":
if song_uri in jscon["blacklist"]:
await ctx.send("That song is blacklisted.")

elif duration > 17:
await ctx.send("Send a shorter song please! :)")
else:
sp.add_to_queue(song_uri)
await ctx.send(
f"Your song ({song_name} by {', '.join(song_artists_names)}) [ {data['external_urls']['spotify']} ] has been added to {ctx.channel.name}'s queue!"
)

def read_json(self, filename):
with open(f"{cwd}/{filename}.json", "r") as file:
Expand Down
19 changes: 19 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
print("\n--------------------------")
print("\n" * 10)

print("....Writing to `.env`")
with open(".env", "a") as env_file:
env_file.write(
f"TOKEN={token}\n"
Expand All @@ -71,14 +72,32 @@
+ "# Set your 'redirect_uri' and 'website' on your Spotify application to 'http://localhost:8080' (Don't change the spotify_redirect_uri in .env)"
)

print("Finished writing to `.env`")

print("....Writing to `config.json`")
with open("config.json", "a") as config_file:
config_file.write(
json.dumps({"nickname": bot_name, "prefix": prefix, "channels": [channel]})
)

print("Finished writing to `config.json`")

print("....Writing to `blacklist.json`")

with open("blacklist.json", "a") as blacklist_file:
blacklist_file.write(json.dumps({"blacklist": []}))

print("Finished writing to `blacklist.json`")

print("....Writing to `blacklist_user.json`")

with open("blacklist_user.json", "a") as blacklist_user_file:
blacklist_user_file.write(json.dumps({"users": []}))

print("Finished writing to `blacklist_user.json`")

print("\n" * 10)

input(
"All done!, enjoy using your Song Request Bot!\nAll you have to do now, is run the `bot.py` file!"
)

0 comments on commit b5240a3

Please sign in to comment.