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
66 changes: 66 additions & 0 deletions docker-compose.postgre.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
version: '3.4'

services:

# The Java bot process -- uses Dockerfile in ./docker/
# Will attempt to restart 3 times.
bot:
image: ib.ai
restart: on-failure:3
build:
context: ./
dockerfile: Dockerfile
env_file:
- bot.env
depends_on:
- db

# Redis database
# Running on port 6379, on a failure it will restart itself
# Connect via the URL 'redis://db:6379'
db:
restart: always
image: redis
volumes:
- db-data:/data

postgredb:
restart: always
image: postgres
volumes:
- postgre-data:/data
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=password
pgadmin:
image: dpage/pgadmin4
restart: always
environment:
- PGADMIN_DEFAULT_EMAIL=ibo@discord.com
- PGADMIN_DEFAULT_PASSWORD=ibodiscord
- PGADMIN_LISTEN_PORT=80
ports:
- "80:80"
volumes:
- pgadmin-data:/data
tty: true
links:
- "postgredb:pgsql-server"

# Automated backup of Redis image to S3 object storage
# backup:
# image: pants1/docker-volume-backup
# restart: on-failure
# env_file:
# - backup.env
# volumes:
# - /var/lib/docker/volumes/:/HostVolumeData
# depends_on:
# - db

volumes:
# Database persistence
db-data:
postgre-data:
pgadmin-data:
22 changes: 11 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
restart: on-failure:3
build:
context: ./
dockerfile: prod.Dockerfile
dockerfile: Dockerfile
env_file:
- bot.env
depends_on:
Expand All @@ -25,16 +25,16 @@ services:
- db-data:/data

# Automated backup of Redis image to S3 object storage
backup:
image: pants1/docker-volume-backup
restart: on-failure
env_file:
- backup.env
volumes:
- /var/lib/docker/volumes/:/HostVolumeData
depends_on:
- db
# backup:
# image: pants1/docker-volume-backup
# restart: on-failure
# env_file:
# - backup.env
# volumes:
# - /var/lib/docker/volumes/:/HostVolumeData
# depends_on:
# - db

volumes:
# Database persistence
db-data:
db-data:
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,10 @@
<version>16.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.14</version>
</dependency>
</dependencies>
</project>
2 changes: 2 additions & 0 deletions src/main/java/com/ibdiscord/IBai.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.ibdiscord.command.registry.CommandRegistry;
import com.ibdiscord.data.LocalConfig;
import com.ibdiscord.data.db.DataContainer;
import com.ibdiscord.data.db.DataContainerPostgres;
import com.ibdiscord.exceptions.JavaVersionException;
import com.ibdiscord.i18n.LocaleException;
import com.ibdiscord.i18n.LocaliserHandler;
Expand Down Expand Up @@ -79,6 +80,7 @@ public static void main(String[] args) throws JavaVersionException {
private void init() {
config = new LocalConfig();
DataContainer.INSTANCE.connect();
DataContainerPostgres.INSTANCE.connect();
try {
LocaliserHandler.INSTANCE.initialize(new File(config.getLangBase()));
} catch(IOException | LocaleException exception) {
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/com/ibdiscord/command/actions/HelperActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* Copyright 2020 Nathaneal Varghese
*
* This file is part of IB.ai.
*
* IB.ai is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* IB.ai is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with IB.ai. If not, see http://www.gnu.org/licenses/.
*/

package com.ibdiscord.command.actions;

import com.ibdiscord.command.CommandAction;
import com.ibdiscord.command.CommandContext;
import com.ibdiscord.utils.UInput;
import net.dv8tion.jda.api.entities.GuildChannel;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.PermissionOverride;
import net.dv8tion.jda.api.entities.Role;

import java.util.List;
import java.util.stream.Collectors;

public class HelperActivity implements CommandAction {

/**
* Returns number of helper messages.
* @param context The command context.
*/
@Override
public void accept(CommandContext context) {
context.assertArguments(1, "error.generic_syntax_arg");

Member member = UInput.getMember(context.getGuild(), context.getArguments()[0]);

if (member == null) {
context.replyI18n("error.helper_invalid");
return;
}

List<String> roles = member.getRoles().stream()
.map(Role::getName)
.filter(roleName -> roleName.toLowerCase().endsWith("helper"))
.collect(Collectors.toList());

roles.forEach(role -> {
List<GuildChannel> subjectChannels = context.getGuild().getChannels().stream()
.filter(guildChannel -> {
PermissionOverride perm = guildChannel.getRolePermissionOverrides().stream()
.filter(permissionOverride -> permissionOverride.getRole().getName().equals(role))
.findFirst().orElse(null);
return perm != null;
})
.collect(Collectors.toList());
});
}
}
73 changes: 73 additions & 0 deletions src/main/java/com/ibdiscord/data/db/DataContainerPostgres.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* Copyright 2020 Nathaneal Varghese
*
* This file is part of IB.ai.
*
* IB.ai is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* IB.ai is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with IB.ai. If not, see http://www.gnu.org/licenses/.
*/

package com.ibdiscord.data.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.EnumSet;

public enum DataContainerPostgres {

/**
* Singleton instance of container.
*/
INSTANCE;

private final String username = "postgres";
private final String password = "password";

/**
* Connect to the database.
*/
public void connect() {
try {
Connection connection = DriverManager.getConnection("jdbc:postgresql://postgredb/", username, password);
Statement statement = connection.createStatement();
String sql = "CREATE DATABASE ibai";
statement.executeUpdate(sql);
System.out.println("Created IB-ai");
statement.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
try {
Connection conn = DriverManager.getConnection("jdbc:postgresql://postgredb/ibai", username, password);
System.out.println("Connected to the PostgreSQL server successfully.");

Statement statement = conn.createStatement();

EnumSet.allOf(SQLQuery.class).forEach(query -> {
if (query.name().startsWith("CREATE_")) {
try {
statement.addBatch(query.toString());
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
});

statement.executeBatch();
statement.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
Loading